编辑以回答最新问题
您可以避免使用意图搜索配对设备。当连接到未配对的设备时,会弹出一条通知,要求配对设备。配对后,这些设备不会再显示此消息,连接应该是自动的(根据您编写程序的方式)。
我使用 Intent 来启用蓝牙并让我的设备可被发现,然后我设置我的代码以进行连接,然后按下按钮进行连接。在您的情况下,您需要确保您的配件也可以被发现。在我的情况下,我使用唯一的 UUID,并且两个设备都必须识别它才能连接。仅当您对两种设备都进行编程时才能使用此功能,无论两者都是 android 还是一种 android 和另一种设备类型。
试试这个,看看能不能解决你的问题。
这个答案是对原始问题的回答,然后才被编辑为另一个问题。
为了清楚起见,我已经编辑了我的答案,正如我从 cmets 中看到的那样,它具有误导性。你的问题有两个部分。
在我的 MotoG (KitKat) 上,如果我打开蓝牙,它会自动连接
到设备(如果它在附近并已配对...)但在我的 LG G3 上我必须去
到配置/蓝牙/配对设备/,然后点击设备以
连接...我想避免这种情况...应该可以吗?
这不是编程问题,而是平台问题。
Android 5.0 中存在一个有据可查的错误,蓝牙无法自动连接以及许多其他 BT 问题。这些问题在 5.0 上的所有更新中继续存在。版本,直到 5.1 才修复。升级。
http://www.digitaltrends.com/mobile/android-lollipop-problems/11/
http://forums.androidcentral.com/lg-g3/473064-bluetooth-streaming-choppy-lg-3-lollipop.html
首先要更新到 5.1
这些问题已在 Lollipop 更新 5.1 中得到解决
http://www.reddit.com/r/Android/comments/306m3y/lollipop_51_bluetooth/
编辑:
我不相信这会解决你的自动配对问题,你想知道如何使用 BTGatt。
如果我输入设备,我已经看到了。检查我能做什么让我
connectGatt() 表示 /.../
但我无法弄清楚如何做到这一点......
使用 BluetoothGatt
https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html
此类提供蓝牙 GATT 功能以启用
与蓝牙智能或智能就绪设备通信。
/.../
可以使用蓝牙设备发现支持 GATT 的设备
发现或 BLE 扫描过程。
https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html
这是一个很好的例子,说明如何使用 BluetoothGatt(它使用听力速率):
https://github.com/googlesamples/android-BluetoothLeGatt/blob/master/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java
我在这里复制了一些代码,以防链接失效。
它基本上遵循与常规蓝牙连接类似的线路。您需要发现并找到支持的设备。
监控状态等
这是与 gatt 最相关的两个特性。
回调:
// Implements callback methods for GATT events that the app cares about. For example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};
广播:
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
// This is special handling for the Heart Rate Measurement profile. Data parsing is
// carried out as per profile specifications:
// http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
}
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
} else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
}
}
sendBroadcast(intent);
}
这个问题也有一些相关代码,学习的时候可能会有所帮助:
BLuetooth Gatt Callback not working with new API for Lollipop
现在问题来了。您的设备是否支持智能蓝牙或智能设备?
此链接提供了一个很棒的智能设备列表。当你实施你的程序时,你也会发现。
http://www.bluetooth.com/Pages/Bluetooth-Smart-Devices-List.aspx