Skip to content

Instantly share code, notes, and snippets.

@t-oster
Created February 20, 2015 14:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-oster/68a568ac4c4e133f67ac to your computer and use it in GitHub Desktop.
Save t-oster/68a568ac4c4e133f67ac to your computer and use it in GitHub Desktop.
This DroidScript (JavaScript for Android) makes my China Bluetooth Bracelet Vibrate whenever I want.
var btn;
var bt;
var txt;
//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a button 1/3 of screen width and 1/4 screen height.
btn = app.CreateButton( "Connect", 0.4, 0.15 );
btn.SetOnTouch( btn_OnTouch );
lay.AddChild( btn );
app.AddLayout( lay );
app.Vibrate("0,100");
txt = app.CreateText( "Bluetooth", 0.8, 0.2, "Multiline" );
lay.AddChild( txt );
}
var lines = [];
function log(text)
{
lines.push(text);
while (lines.length > 5)
{
lines.shift();
}
var line = "";
for (var i in lines)
{
line += lines[i]+"\n";
}
txt.SetText(line);
}
function send(cmd)
{
bt.Write(String.fromCharCode(13)+String.fromCharCode(10)+cmd+String.fromCharCode(13)+String.fromCharCode(10));
log("> "+cmd);
}
var connected = 0;
channel = 0;
//Called when user touches the button.
function btn_OnTouch()
{
if (connected == 0)
{
txt.SetText("");
log("connecting...");
//Create Bluetooth serial object.
bt = app.CreateBluetoothSerial();
bt.SetOnConnect( bt_OnConnect )
bt.SetOnReceive( bt_OnReceive );
//bt.SetSplitMode( "End", "\r" );
bt.Connect("BRACELET",channel);//"00:19:04:0C:78:5F" "0000110800001000800000805F9B34FB");
}
else
{
send("RING");
}
}
//Called when we are connected.
function bt_OnConnect( ok)
{
if( ok )
{
log("connected."+channel);
connected = 1;
btn.SetText("RING");
}
else
{
log( "Failed to connect!" );
bt.Disconnect();
channel++;
}
}
function bt_OnReceiveLine(line)
{
log("< "+line);
if (line.indexOf("BRSF") >= 0)
{
send("+BRSF:0");
send("OK");
}
if (line.indexOf("CIND=") >= 0)
{
send("+CIND: (\"service\",(0,1)),(\"call\",(0,1))");
send("OK");
}
if (line.indexOf("CIND?") >= 0)
{
send("+CIND: 1,0");
send("OK");
}
if (line.indexOf("CMER") >= 0)
{
send("OK");
}
if (line.indexOf("CHLD=?") >= 0)
{
send("+CHLD: 0");
send("OK");
}
}
var buf = "";
//Called when we get data from device.
function bt_OnReceive( data )
{
if (data === "\r" || data == "\n" || data == "")
{
bt_OnReceiveLine(buf);
buf = "";
}
else
{
buf += data;
}
}
@shimondoodkin
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment