Introduction to Android Bluetooth Development



1. Response permission for Bluetooth module in Android
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Android.permission. BLUETOOTH :
  Allow the program to connect to the paired Bluetooth device, request to connect/receive connection/transfer data requires permission, mainly used to operate after pairing;
android.permission.BLUETOOTH_ADMIN :
  Allow the program to discover and pair Bluetooth devices, this permission is used To manage Bluetooth devices, with this permission, the application can use the local Bluetooth device, mainly for the operation before pairing;

priority: BLUETOOTH permission is the premise of BLUETOOTH_ADMIN permission, if there is no BLUETOOTH permission, BLUETOOTH_ADMIN permission cannot be used;

2. Set the Bluetooth of the machine through the BluetoothAdapter
a. Get the BluetoothManager from the systemService and get the BluetoothAdapter.getDefaultAdapter directly
  from the BluetoothManager.getAdapter
or
  directly through the static method of the
  BluetoothAdapter

  //Turn on the Bluetooth directly 
  adapter.enable(); 
  //Directly close the Bluetooth 
  adapter.disable();

3. Set Bluetooth through the system settings interface
  //Directly open the system's Bluetooth settings panel, open or close
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
startActivityForResult(intent, 0x1); 
The result can be processed in the onActivityResult() method in the Activity. If the Bluetooth module is successfully turned on, the result will be returned RESULT_OK; If the Bluetooth module has failed to be turned on, the result code RESULT_CANCELED will be returned;


4. Switch status value constant Introduction
Bluetooth is off: int STATE_OFF, the value is 10, the Bluetooth module is off;
Bluetooth is on: int STATE_TURNING_ON, the value is 11, the Bluetooth module is on;
Bluetooth is on: int STATE_ON, the value is 12, the Bluetooth module is on;
Bluetooth Turning on: int STATE_TURNING_OFF , the value is 13, the bluetooth module is closing;
bluetooth switch state sequence: STATE_OFF --> STATE_TURNING_ON --> STATE_ON --> STATE_TURNING_OFF --> STATE_OFF
to turn on and off the bluetooth module, can be monitored through ACTION_STATE_CHANGED broadcast

5.//Turn on the Bluetooth discovery function of the machine (default is 120 seconds, the time can be extended to 300 seconds at most) 
ACTION_REQUEST_DISCOVERABLE, make Bluetooth visible, the value is "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", the default visible time For 120s, you can add an additional field to the broadcast, set any visible time, the additional field is EXTRA_DISCOVERABLE_DURATION, and set the duration (up to 300 seconds); BLUETOOTH permission is required;
the result can be processed in the onActivityResult() method in the Activity, if the Bluetooth If the module setting is visible successfully, return the result RESULT_OK; if the Bluetooth module setting is visible and fail, return the result code RESULT_CANCELED;
[java] view plain copy
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0 ); 
startActivityForResult(discoverableIntent,resultCode) .

6. Scanning state value
No function state: int SCAN_MODE_NONE , the value is 20, both query scanning and page scanning are invalid, in this state, the Bluetooth module can neither scan other devices nor be visible;
Scanning status: int SCAN_MODE_CONNECTABLE , the value is 21, the query scan is invalid, the page scan is valid, the Bluetooth module can scan other devices in this state, and only visible to the paired Bluetooth device in terms of visibility, only the paired device can actively connect to this Device;
visible state: int SCAN_MODE_CONNECTABLE_DISCOVERABLE, the value is 23, both query scanning and page scanning are valid;

query scanning function: other devices can scan to this device, which means visibility is visible;
page scanning function: can actively scan other devices;


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326831859&siteId=291194637