Using the thinthadapter class, you can search for peripheral Bluetooth devices on Android devices and then pair (BIND) them. Bluetooth communication is based on a unique MAC address for mutual transmission. Security Pairing is required for Bluetooth communication. Then connect to each other. After the connection, the device will share the same RFCOMM channel to transmit data to each other. Currently, these implementations are implemented in the android 2.0 or later SDK. 1. Find and find findding/discovering Devices You can use the startdiscovery () method of the thadapter class to obtain the peripheral bluetooth device in an asynchronous way when detecting a bluetooth device in Android, because it is an asynchronous method, we do not need to consider the thread blocking problem. The whole process takes about 12 seconds. Then we register a broadcastreceiver object to receive the information of the searched bluetooth device, we filter the intent action action_found to obtain the detailed information of each remote device. Additional parameters include extra_device and extra_class In the intent field, which contain the device type of each ththdevice object and object, for example: Code Private Final broadcastreceiver cwjreceiver = new broadcastreceiver (){ Public void onreceive (context, intent ){ String action = intent. getaction (); If (descrithdevice. action_found.equals (Action )){ Effecthdevice device = intent. getparcelableextra (effecthdevice. extra_device ); Myarrayadapter. Add (device. getname () + "android123" + device. getaddress (); // obtain the device name and MAC address } } }; // Register this broadcastreceiver Intentfilter filter = new intentfilter (effecthdevice. action_found ); Registerreceiver (cwjreceiver, filter ); Finally, android123 reminds everyone to note that remember to rewrite the ondestory () method in service or activity and use the unregisterreceiver method to register this broadcastreceiver object to ensure that resources are correctly recycled. Some other status changes include extra_scan_mode_changed, extra_scan_mode, extra_previus_scan_mode, scan_mode_connectable_discoverable, scan_mode_connectable, and scan_mode_none, 2. Bind bnded/encrypted red Device To pair a bluetooth device in Android, you can call the getbondeddevices () method of the thadapter class to obtain the paired device. This method returns an array of paired thdevices to distinguish each paired device, the sample code is as follows: Set primary reddevices = cwj1_thadapter. getbondeddevices (); If (repeated reddevices. Size ()> 0) // If the obtained result is greater than 0, resolution is started one by one. { For (incluthdevice device: inclureddevices ){ Myarrayadapter. Add (device. getname () + "android123" + device. getaddress (); // obtain the name and MAC address of each device and add it to the array adapter myarrayadapter. } } Many netizens do not understand how to make their own Mobile phone How to configure the settings discovered by other Bluetooth devices? Let's talk about it together. Iii. Enabling discoverability If you need to confirm the operation and do not need to obtain the underlying Bluetooth service instance, you can use an intent to pass the action_request_discoverable parameter. Here, you can use startactivityforresult to forcibly obtain a result and override startactivityforresult () method to obtain the execution result. The returned results include result_ OK and result_cancelled, respectively, indicating that the operation is enabled or canceled (failed). Of course, the simplest method is to directly execute the command. The sample code is as follows: Intent cwjintent = new intent (effecthadapter. action_request_discoverable ); Cwjintent. putextra (effecthadapter. extra_discoverable_duration, 300 ); Startactivity (cwjintent ); Next, the system will prompt whether the user is allowed. The dialog box is as follows: |
This article is transferred fromWww.35java.com