There is no setting application provided in the Android things system, so all Bluetooth processing needs to be maintained by ourselves. Therefore, if we need to use Bluetooth connection, we must use Android to scan, pair, and connect the whole process.
for example:
Bluetooth pairing
unpair
As well as setting pins, connections, etc., need to call a lot of reflection, here I recommend the method to you:
Using the BluetoothConnectionManager class that exists in the things sdk, we know by consulting the documentation that google hereby announces this interface in order to do the Bluetooth adaptation on things
Bluetooth pairing example:
import android.bluetooth.BluetoothDevice; import com.google.android.things.bluetooth.BluetoothConnectionManager; import com.google.android.things.bluetooth.BluetoothPairingCallback; import com.google.android.things.bluetooth.PairingParams; ... public class PairingActivity extends Activity { BluetoothConnectionManager mBluetoothConnectionManager; @Override protected void onCreate(Bundled savedInstanceState) { super.onCreate (savedInstanceState); mBluetoothConnectionManager = BluetoothConnectionManager.getInstance(); mBluetoothConnectionManager.registerPairingCallback(mBluetoothPairingCallback); } @Override protected void onDestroy() { super.onDestroy (); mBluetoothConnectionManager.unregisterPairingCallback(mBluetoothPairingCallback); } private void startPairing(BluetoothDevice remoteDevice) { mBluetoothConnectionManager.initiatePairing(remoteDevice); } private BluetoothPairingCallback mBluetoothPairingCallback = new BluetoothPairingCallback() { @Override public void onPairingInitiated(BluetoothDevice bluetoothDevice, PairingParams pairingParams) { // Handle incoming pairing request or confirmation of outgoing pairing request handlePairingRequest(bluetoothDevice, pairingParams); } @Override public void onPaired(BluetoothDevice bluetoothDevice) { // Device pairing complete } @Override public void onUnpaired(BluetoothDevice bluetoothDevice) { // Device unpaired } @Override public void onPairingError(BluetoothDevice bluetoothDevice, BluetoothPairingCallback.PairingError pairingError) { // Something went wrong! } }; }
Example of Bluetooth connection:
import android.bluetooth.BluetoothDevice; import com.google.android.things.bluetooth.BluetoothConnectionManager; import com.google.android.things.bluetooth.BluetoothConnectionCallback; import com.google.android.things.bluetooth.BluetoothProfile; import com.google.android.things.bluetooth.ConnectionParams; ... public class ConnectActivity extends Activity { BluetoothConnectionManager mBluetoothConnectionManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); mBluetoothConnectionManager = BluetoothConnectionManager.getInstance(); mBluetoothConnectionManager.registerConnectionCallback(mBluetoothConnectionCallback); } @Override protected void onDestroy() { super.onDestroy (); mBluetoothConnectionManager.unregisterConnectionCallback(mBluetoothConnectionCallback); } private void connectToA2dp(BluetoothDevice bluetoothDevice) { mBluetoothConnectionManager.connect(bluetoothDevice, BluetoothProfile.A2DP_SINK); } // Set up callbacks for the profile connection process. private final BluetoothConnectionCallback mBluetoothConnectionCallback = new BluetoothConnectionCallback() { @Override public void onConnectionRequested(BluetoothDevice bluetoothDevice, ConnectionParams connectionParams) { // Handle incoming connection request handleConnectionRequest(); } @Override public void onConnectionRequestCancelled(BluetoothDevice bluetoothDevice, int requestType) { // Request cancelled } @Override public void onConnected(BluetoothDevice bluetoothDevice, int profile) { // Connection completed successfully } @Override public void onDisconnected(BluetoothDevice bluetoothDevice, int profile) { // Remote device disconnected } }; }
Please refer to the official documentation
https://developer.android.google.cn/things/sdk/apis/bluetooth.html
-------------------------------------------------------------------------
Note: NXP Pico i.MX7D please be sure to plug in the antenna before debugging