Talking about Bluetooth development and talking about bluetooth

Source: Internet
Author: User

Talking about Bluetooth development and talking about bluetooth

The project uses Bluetooth development to obtain data from hardware sensors.

Because I have no experience in Bluetooth development, I decided to first understand some Bluetooth development knowledge and then read the Bluetooth code written by my colleagues.

 

Bytes ---------------------------------------------------------------------------------------------------

I. Bluetooth Development

1,Descrithadapter

Obviously, the Bluetooth adapter.

Use this class to perform the following operations:

1. enable or disable the bluetooth device. 2. Scan the bluetooth device. 3. Set/obtain the Bluetooth status information, such as the Bluetooth status value, Bluetooth Name, and Bluetooth Mac address;

 

2,Descrithdevice

A Bluetooth device is the device we connect.

Method:

Export thdata. SENSOR_DOWN_ADRESS = "20: 16: 05: 25: 32: 31"; // MAC address
BluetoothDevice sensor_down = mBluetoothAdapter.getRemoteDevice(BluetoothData.SENSOR_DOWN_ADRESS);

 

 

Bytes ---------------------------------------------------------------------------------------------------

2. Basic Steps for Bluetooth Development and Use

1. Permissions

This is required to add permissions to the AndroidManifest. xml file.

// Use the Bluetooth function in the Program <uses-permission android: name = "android. permission. BLUETOOTH "/> // start the device to discover or manipulate BLUETOOTH Settings <uses-permission android: name =" android. permission. BLUETOOTH_ADMIN "/>

 

2. Obtain the Bluetooth adapterReturns the thadapter object and determines whether the current device supports Bluetooth.

BluetoothAdapter mBluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();
If (mblustmthadapter = null ){
// The device does not support Bluetooth.
Toast. makeText (this, "the current device does not support Bluetooth", Toast. LENGTH_SHORT). show ();
Return;
}

 

3. When the device supports the Bluetooth function, we need to determine whether the Bluetooth function is enabled. If it is not enabled, We need to enable it.

IsEnabled () determines whether the Bluetooth function is enabled. enable () is used to enable the Bluetooth function.

If (! Mblustmthadapter. isEnabled () {boolean enable = mblustmthadapter. enable (); // The returned value indicates whether the Bluetooth function is enabled. if (enable) {Toast. makeText (this, "Bluetooth enabled successfully! ", Toast. LENGTH_SHORT). show () ;}else {Toast. makeText (this," failed to enable the Bluetooth function. Please go to 'System settings' to manually enable the Bluetooth function! ", Toast. LENGTH_SHORT). show (); return ;}}

 

4. query the paired bluetooth device

Set <symbol thdevice> symbol reddevices = mbluw.thadapter. getBondedDevices (); // if there is a paired device if (pairedDevices. size ()> 0) {ArrayList <String> mArrayAdapter = new ArrayList <> (); for (incluthdevice device: inclureddevices) {// Add the device name and address mArrayAdapter in the list using array adapter. add (device. getName () + "\ n" + device. getAddress (); Log. I ("bluetooth", device. getName () + "\ n" + device. getAddress () ;}} else {Toast. makeText (this, "no paired devices available", Toast. LENGTH_SHORT ). show ();}

 

5. query available nearby bluetooth devices

The method is simple.

 

mBluetoothAdapter.startDiscovery();

Note that this method is executed asynchronously, which is equivalent to enabling a sub-thread. The whole process takes about 12 seconds.

Remember, when we search and successfully connect to the device we need, we need to disable the search behavior in time and use cancelDiscovery.

 

Next, we need to write a broadcast to receive the queried device data.

Private final BroadcastReceiver mReceiver = new BroadcastReceiver () {public void onReceive (Context context, Intent intent) {String action = intent. getAction (); all1_thdevice = new ArrayList <> (); if (then thdevice. ACTION_FOUND.equals (action) {descrithdevice device = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE); Log. I ("bluetooth", "paired devices:" + device. getName () + "\ n" + device. getAddress ());}}};

Remember to register broadcast in the onCreate () method:

 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter);

 

Related Article

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.