Android things系统中并没有提供setting application,所以所有的蓝牙处理需要我们自行维护,故如果我们需要使用蓝牙连接必须使用Android 自己去做扫描,配对,连接整个过程
比如:
蓝牙配对
解除配对
以及设置pin,连接等,都需要调用大量的反射,在这里我给大家推荐的方法是:
使用things sdk中存在的BluetoothConnectionManager类,通过查阅文档得知,google为了做things上的蓝牙适配,特此对外公布了此接口
蓝牙配对示例:
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!
}
};
}
蓝牙连接示例:
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
}
};
}
官方文档请查阅
https://developer.android.google.cn/things/sdk/apis/bluetooth.html
-------------------------------------------------------------------------
注意:NXP Pico i.MX7D请务必插上天线再进行调试