Android Developer--one of the examples of Bluetooth development
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 14px;"><strong>First Step: declaring Bluetooth Permissions</strong></span></p></p><pre><pre> <span style="color: #008000;"><!--</span><span style="color: #008000;"></span>-<span style="color: #008000;"></span> <span style="color: #0000ff;"><</span><span style="color: #ff0000;"></span>android:name = "<span style="color: #800000;"></span><span style="color: #0000ff;"> Android.permission.BLUETOOTH "</span><span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;"></span><span style="color: #ff0000;">android:name</span> <span style="color: #0000ff;">= "android.permission.BLUETOOTH_ADMIN"</span> <span style="color: #0000ff;">/></span></pre></pre><p><p><span style="font-size: 14px;"><strong>Step two: Get Bluetoothadapter to determine if the device supports Bluetooth</strong></span></p></p><pre><pre> Bluetoothadapter mbluetoothadapter =<span style="color: #000000;"> bluetoothadapter.getdefaultadapter (); </span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">) { </span><span style="color: #008000;">//</span> device<span style="color: #008000;"> does </span>not supported Bluetooth<span style="color: #008000;">//</span> <span style="color: #008000;"> Description The device does not support Bluetooth</span> }</pre></pre><p><p><span style="font-size: 14px;"><strong>Step three: Check that the current Bluetooth is turned on</strong></span></p></p><pre><pre><span style="color: #0000ff;">if</span> (! <span style="color: #000000;">mbluetoothadapter.isenabled ()) { </span><span style="color: #0000ff;">New</span> <span style="color: #000000;"> Intent (bluetoothadapter.action_request_enable); Startactivityforresult (enablebtintent, request_enable_bt); </span> <span style="color: #008000;">//</span> do <span style="color: #008000;">not prompt, force open </span> <span style="color: #008000;">//</span> <span style="color: #008000;">madapter.enable ();</span> }</pre></pre><p><p>If this is the first way: a popup window will appear</p></p><p><p>A dialog would appear requesting user permission to enable Bluetooth, as shown in Figure 1. If the user responds "Yes," the system would begin to enable Bluetooth and focus would return to your application once the P Rocess completes (or fails).</p></p><p><p>If enabling Bluetooth succeeds, your Activity would receive the <code>RESULT_OK</code> result code in the <code>onActivityResult()</code> callback. If Bluetooth is not enabled due to a error (or the user responded "No") then the result code would be <code>RESULT_CANCELED</code> .</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onactivityresult (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>requestcode,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">resultcode, Intent Data) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(requestcode = =<span style="color: #000000;"><span style="color: #000000;">Request_enable_bt) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(resultcode = =<span style="color: #000000;"><span style="color: #000000;">Result_ok) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">bluetooth is turned</span> on</span>LOG.D ("h_bl", "bluetooth is turned on"<span style="color: #000000;"><span style="color: #000000;">); String Bluetoothname</span></span>= Bluetoothadapter.getname ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get local bluetooth name</span></span>String bluetoothaddress = bluetoothadapter.getaddress ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get local Bluetooth address</span></span><span style="color: #000000;"><span style="color: #000000;">Tv_bluetoothname.append (bluetoothname); Tv_bluetoothaddress.append (bluetoothaddress); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{LOG.D (</span></span>"h_bl", "bluetooth turned on failed"<span style="color: #000000;"><span style="color: #000000;">); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onactivityresult (requestcode, resultcode, data); }</span></span></pre><p><p>which</p></p><pre><pre><span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">int</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Bluetooth Open Request Code</span></pre></pre><p><p>optionally, your application can also listen <code>ACTION_STATE_CHANGED</code> for the broadcast Intent, which the system would broadcast whenever the Bluetooth State has Changed. This broadcast contains the extra fields <code>EXTRA_STATE</code> <code>EXTRA_PREVIOUS_STATE</code> and, containing the new and old Bluetooth states, respectively. Possible values for these extra fields is <code>STATE_TURNING_ON</code> , <code>STATE_ON</code> , <code>STATE_TURNING_OFF</code> , and <code>STATE_OFF</code> . Listening for this broadcast can being useful to detect changes made to the Bluetooth state while your app is Running.</p></p><p><p>optionally, your app can listen to the <span style="color: #ff0000;">action_state_changed broadcast Intent</span>, and when the system Bluetooth status changes it will initiate this broadcast, which contains extra_state and Extra_previous_ State additional fields that contain the new and old Bluetooth states, respectively, may have State_turning_on,state_on,state_turning_off and state_off possible values, and listening broadcasts can be useful for detecting Bluetooth status Changes. --- <span style="color: #ff0000;">to determine if Bluetooth is turned</span> on</p></p><p><p><strong>Tip:</strong> Enabling discoverability'll automatically enable Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 Above. Read about enabling discoverability, BELOW.</p></p><p><p>Tip: enabling the program will automatically enable Bluetooth. If you play the visibility of the <span style="color: #000000;">device</span> , you can skip the 2 steps Above. Read about enabling discoverable, BELOW. --- <span style="color: #ff0000;">setting the visibility of Bluetooth</span></p></p><p><p><span style="font-size: 14px;"><strong>Fourth Step: find Other Devices</strong></span></p></p><p><p>With bluetoothadapter, you can turn on the <span style="color: #ff0000;">visibility of the device (the Bluetooth device you are searching For)</span> and the <span style="color: #ff0000;">list of devices that are already paired</span> .</p></p><p><p>Remember the <span style="color: #ff0000;">difference</span> between <span style="color: #ff0000;">pairing</span> and <span style="color: #ff0000;">connection</span> , already paired means that both devices are aware of each Other's presence, already have a shared key used to authenticate, and can then be captured for establishing a trusted connection to each Other.</p></p><p><p>4.1 Find a paired device</p></p><pre>Set<bluetoothdevice> paireddevices =<span style="color: #000000;"><span style="color: #000000;">mbluetoothadapter.getbondeddevices (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If There is paired devices</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(paireddevices.size () > 0<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Loop through paired devices</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(bluetoothdevice device:paireddevices) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">ADD the name and address to an array adapter to show in a</span></span><span style="color: #008000;"><span style="color: #008000;">ListView</span></span>Marrayadapter.add (device.getname () + "\ n" +<span style="color: #000000;"><span style="color: #000000;">device.getaddress ()); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{toast.maketext (getapplicationcontext (),</span></span>"no equipment has been found for the pair!" "<span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); }</span></span></pre><p><p>4.2 Discovery Devices</p></p><p><p>Call<code>startDiscovery()函数后,系统将扫描12秒,返回找到的蓝牙设备。我们需用广播来接收。</code></p></p><p><p>For each device, the system would broadcast the <code>ACTION_FOUND</code> Intent. This Intent carries the extra fields <code>EXTRA_DEVICE</code> <code>EXTRA_CLASS</code> and, containing a <code>BluetoothDevice</code> and a <code>BluetoothClass</code> , respectively. For example, Here's How can register to handle the broadcast when devices is discovered:</p></p><p><p>Each device is found, the system will send <code>ACTION_FOUND</code> Intent. broadcast, <span style="color: #ff0000;">This Intent carries extra fields <code><span style="color: #ff0000;">EXTRA_DEVICE</span></code> <code><span style="color: #ff0000;">EXTRA_CLASS</span>,包含a <code><span style="color: #ff0000;">BluetoothDevice</span></code> and a <code><span style="color: #ff0000;">BluetoothClass</span>。</code></code> </span> and</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a broadcastreceiver for Action_found</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Broadcastreceiver Mreceiver =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">broadcastreceiver () {</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;">onreceive (context context, Intent Intent) {String Action</span></span>=<span style="color: #000000;"><span style="color: #000000;">intent.getaction (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">when <span style="color: #008000;">Discovery finds a device</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(BluetoothDevice.ACTION_FOUND.equals (ACTION)) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get The Bluetoothdevice object</span> from the Intent</span>Bluetoothdevice device =<span style="color: #000000;"><span style="color: #000000;">Intent.getparcelableextra (bluetoothdevice.extra_device); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">ADD the name and address to an array adapter to show in a ListView</span></span>Marrayadapter.add (device.getname () + "\ n" +<span style="color: #000000;"><span style="color: #000000;">device.getaddress ()); } }};</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Register the Broadcastreceiver</span></span>Intentfilter filter =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">intentfilter (bluetoothdevice.action_found); registerreceiver (mreceiver, filter);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Don ' t forget to unregister during OnDestroy</span></span></pre><p><p>Where you need to call the</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Scan Bluetooth device</span> btadapter.startdiscovery ();</pre></pre><p><p>Note: Once You has found a device to connect, be certain, and always stop discovery with <code>cancelDiscovery()</code> before attempting a conne Ction.</p></p><p><p>Before connecting the device, call Canceldiscovery () to conserve resources.</p></p><p><p>In addition, you need to cancel the Broadcast:</p></p><pre><pre><span style="color: #000000;"> @Override </span><span style="color: #0000ff;">protected</span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> OnDestroy () { unregisterreceiver (mreceiver); </span> <span style="color: #0000ff;">Super</span><span style="color: #000000;">. OnDestroy (); }</span></pre></pre><p><p></p></p><p><p><span style="font-size: 14px;"><strong>Fifth step: the discoverable time setting of the device, the default is 120S.</strong></span></p></p><pre><pre><span style="color: #0000ff;"></span><span style="color: #000000;"></span><span style="color: #000000;">startactivity (discoverableintent)</span> ;</pre></pre><p><p></p></p><p><p>Android Developer--one of the examples of Bluetooth development</p></p></span>
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