BLE Android App control LED light implementation

Source: Internet
Author: User
<span id="Label3"></p><p><p>Source: BLE Android App control LED light implementation</p></p><p><p>Note: refer to Amomcu source code Modification.<br><br>Open the app and check if Bluetooth is turned on</p></p><pre><span style="color: #000000;"><span style="color: #000000;">Bluetoothadapter mbluetoothadapter;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Bluetoothmanager Bluetoothmanager =<span style="color: #000000;"><span style="color: #000000;">(bluetoothmanager) Getsystemservice (context.bluetooth_service); Mbluetoothadapter</span></span>=<span style="color: #000000;"><span style="color: #000000;">Bluetoothmanager.getadapter ();</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">ensures Bluetooth is available on the device and it is Enabled. If not,</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">displays a dialog requesting user permission to enable Bluetooth.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mbluetoothadapter = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>|| !<span style="color: #000000;"><span style="color: #000000;">mbluetoothadapter.isenabled ()) {Intent enablebtintent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (bluetoothadapter.action_request_enable); Startactivityforresult (enablebtintent, request_enable_bt);}</span></span></pre><p><p>Bluetooth scan</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Scanledevice (<span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Enable) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(enable) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Stops scanning after a pre-defined scan period.</span></span>Mhandler.postdelayed (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {mscanning</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; Mbluetoothadapter.stoplescan (mlescancallback); }}, scan_period); Mscanning</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Mbluetoothadapter.startlescan (mlescancallback); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{mscanning</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; Mbluetoothadapter.stoplescan (mlescancallback); } }</span></span></pre><p><p>Connecting GATT Services<br><span style="white-space: pre;">Binding a service</span></p></p><p><p></p></p><pre>Intent gattserviceintent =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Bluetoothleservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); Bindservice (gattserviceintent, mserviceconnection, bind_auto_create); note: bluetoothleservice.java, which belongs to Android source code, can be downloaded back Online. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Code to manage Service lifecycle.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Serviceconnection mserviceconnection =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">serviceconnection () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onserviceconnected (componentname componentname, ibinder service) {mbluetoothleservice</span></span>=<span style="color: #000000;">( <span style="color: #000000;">(bluetoothleservice.localbinder) service). GetService (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">mbluetoothleservice.initialize ()) {LOG.E (TAG,</span></span>"unable to initialize Bluetooth"<span style="color: #000000;"><span style="color: #000000;">); Finish (); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">automatically connects to the device upon successful start-up Initialization.</span></span><span style="color: #000000;"><span style="color: #000000;">Mbluetoothleservice.connect (mdeviceaddress); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onservicedisconnected (componentname Componentname) {mbluetoothleservice</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } };</span></span></pre><p><p><span style="white-space: pre;">Click the response of a feature value:<br>Get the previous data, do not scan again, from the scanned page to get the data, pass over the</span></p></p><pre>b=<span style="color: #000000;"><span style="color: #000000;">getintent (). getextras (); tv_addr.settext (b.getstring (extras_device_address)); mdeviceaddress</span></span>=<span style="color: #000000;"><span style="color: #000000;">b.getstring (extras_device_address); tv_name.settext (b.getstring (extras_device_name)); mDeviceName</span></span>=<span style="color: #000000;"><span style="color: #000000;">b.getstring (extras_device_name); tv_rssi.settext (b.getstring (extras_device_rssi)); LV</span></span>= (expandablelistview)<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">. Findviewbyid (r.id.expandablelistview1); lv.setonchildclicklistener (serviceslistclicklistner);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Expandablelistview.onchildclicklistener Serviceslistclicklistner =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Expandablelistview.onchildclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Onchildclick (expandablelistview parent, View v,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">groupposition,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>childposition,<span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span><span style="color: #000000;"><span style="color: #000000;">Id) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mgattcharacteristics! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Bluetoothgattcharacteristic characteristic =<span style="color: #000000;"><span style="color: #000000;">mgattcharacteristics.get (groupposition). Get (childposition); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">current target feature value</span></span>Target_chara=<span style="color: #000000;"><span style="color: #000000;">characteristic; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Charaprop =<span style="color: #000000;"><span style="color: #000000;">characteristic.getproperties (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>((charaprop | Bluetoothgattcharacteristic.property_notify) > 0<span style="color: #000000;"><span style="color: #000000;">) {mnotifycharacteristic</span></span>=<span style="color: #000000;"><span style="color: #000000;">characteristic; Mbluetoothleservice.setcharacteristicnotification (characteristic,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); } tv_uuid.settext (characteristic.getuuid (). toString ()); Intent Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (); B.putstring (</span></span>"connet_sate"<span style="color: #000000;"><span style="color: #000000;">, status); B.putstring (</span></span>"UUID"<span style="color: #000000;"><span style="color: #000000;">, Characteristic.getuuid (). toString ()); Intent.putextras (b); Intent.setclass (mygattdetail.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Ledcontrol.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); StartActivity (intent); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; } };</span></span></pre><p><p>Data for reading and writing characteristic values<br>Read Data:</p></p><pre><pre><span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">Static</span> Bluetoothgattcharacteristic mycharacteristic=<span style="color: #0000ff;">null</span><span style="color: #000000;"></span><span style="color: #0000ff;">private</span><span style="color: #0000ff;">static</span> <span style="color: #000000;"> Bluetoothleservice mbluetoothleservice; Mbluetoothleservice.readcharacteristic (mycharacteristic);</span></pre></pre><p><p>Note: readcharacteristic The function calls Bluetoothgatt.readcharacteristic (bluetoothgattcharacteristic Characteristic)<br><br>Write Data:<br><span style="white-space: pre;">Mbluetoothleservice.writecharacteristic (mycharacteristic);<br>Note: writecharacteristic The function calls Bluetoothgatt.writecharacteristic (bluetoothgattcharacteristic Characteristic)<br><br><br>Example Code:</span></p></p><p><p></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>[] buffer =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span>[] {0x6f,0x0f,0x0f,0x0f,0x0f,0x0f<span style="color: #000000;"><span style="color: #000000;">}; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">send () {mygattdetail.write (convertstring (buffer));}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>String convertstring (<span style="color: #0000ff;"><span style="color: #0000ff;">Char</span></span><span style="color: #000000;"><span style="color: #000000;">Buffer[]) {String nrcvstring; StringBuffer Tstringbuf</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuffer (); Tstringbuf.append (buffer); Nrcvstring</span></span>=<span style="color: #000000;"><span style="color: #000000;">tstringbuf.tostring (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">nrcvstring;} Device_name</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.ledpage_devname); ledButton1</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ToggleButton) Findviewbyid (r.id.ledpage_led1); ledbutton1.setoncheckedchangelistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Oncheckedchangelistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">oncheckedchanged (compoundbutton buttonview,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">IsChecked) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isChecked) {buffer[</span></span>1]=0x01<span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{buffer[</span></span>1]=0x00<span style="color: #000000;"><span style="color: #000000;">;} Send ();}}); LedButton2</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ToggleButton) Findviewbyid (r.id.ledpage_led2); ledbutton2.setoncheckedchangelistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Oncheckedchangelistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">oncheckedchanged (compoundbutton buttonview,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">IsChecked) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isChecked) {buffer[</span></span>2]=0x01<span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{buffer[</span></span>2]=0x00<span style="color: #000000;"><span style="color: #000000;">;} Send ();}}); LedButton3</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ToggleButton) Findviewbyid (r.id.ledpage_led3); ledbutton3.setoncheckedchangelistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Oncheckedchangelistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">oncheckedchanged (compoundbutton buttonview,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">IsChecked) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isChecked) {buffer[</span></span>3]=0x01<span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{buffer[</span></span>3]=0x00<span style="color: #000000;"><span style="color: #000000;">;} Send ();}}); LedButton4</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ToggleButton) Findviewbyid (r.id.ledpage_led4); ledbutton4.setoncheckedchangelistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Oncheckedchangelistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">oncheckedchanged (compoundbutton buttonview,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">IsChecked) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isChecked) {buffer[</span></span>4]=0x01<span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{buffer[</span></span>4]=0x00<span style="color: #000000;"><span style="color: #000000;">;} Send ();}});</span></span></pre><p><p><span style="white-space: pre;">Using the ToggleButton control to control leds:</span></p></p><p><p><span style="white-space: pre;">Problem:</span></p></p><p><p><span style="white-space: pre;">Using Ti's Demo will find that turning on an LED is slow and occasionally fails. That is, it will drop the frame, consider the previous write about the delay value of the setting, as Follows:</span></p></p><p><p><span style="white-space: pre;">Connection Interval (gaprole_min_conn_interval && gaprole_max_conn_interval)-connection interval, using the frequency hopping mechanism in the connection of two ble devices. Both devices send and receive data using a specific channel, and then use the new channel (the link layer of the BLE protocol stack to process the channel Switching) over a period of time. Both devices send and receive data after switching channels is called a connection Event. Although no application data is sent and received, the two devices will still exchange the link layer data to maintain the Connection. This connection interval is the time interval between the two connection Events. The interval is a unit of 1.25ms, with 6-3200 of the range of connection intervals between 7.5ms-4s (4000ms).</span></p></p><p><p></p></p><p><p><span style="white-space: pre;"><span style="white-space: pre;"><span style="white-space: pre;">It can be seen that the interval is small and suitable for tasks that require rapid response. The LED lights need to react quickly, so the settings are as Follows:</span></span></span></p></p><p><p><span style="white-space: pre;"><span style="white-space: pre;"><span style="white-space: pre;">Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled</span></span></span></p></p><p><p><span style="white-space: pre;"><span style="white-space: pre;"><span style="white-space: pre;">#define Default_desired_min_conn_interval 6//80</span></span></span></p></p><p><p><span style="white-space: pre;"><span style="white-space: pre;"><span style="white-space: pre;">Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled</span></span></span></p></p><p><p><span style="white-space: pre;"><span style="white-space: pre;"><span style="white-space: pre;">#define Default_desired_max_conn_interval 80//800</span></span></span></p></p><p><p>This way, real-time experience is a big Boost.</p></p><p><p></p></p><p><p>BLE Android App control LED lamp implementation (rpm)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.