블루투스 LE를 통해 상태 장치에 연결하는 Android 애플리케이션을 작성하는 중입니다. 그러나 마치 GATT 서버에 제대로 연결되어 있지 않은 것처럼 보입니다. 몇 초마다 내 로그 파일에 다음과 같이 표시됩니다.Android 블루투스 LE 오류 : 콜백을 등록하지 못했습니다.
03-27 11:33:39.821: D/BluetoothAdapter(26644): onScanResult() - Device=0C:F3:EE:AA:33:35 RSSI=-53
03-27 11:33:39.821: I/BluetoothGattActivity(26644): New LE Device: BT-001 @ -53
03-27 11:33:39.821: I/BluetoothGattActivity(26644): New LE Device matches !!!
03-27 11:33:39.831: I/BluetoothGattActivity(26644): In the fish function.
03-27 11:33:39.831: D/BluetoothGatt(26644): connect() - device: 0C:F3:EE:AA:33:35, auto: false
03-27 11:33:39.831: D/BluetoothGatt(26644): registerApp()
03-27 11:33:39.831: D/BluetoothGatt(26644): registerApp() - UUID=7155a6e0-5432-42cc-8b05-a080c86aaccb
03-27 11:33:39.841: I/BluetoothGatt(26644): Client registered, waiting for callback
03-27 11:33:49.840: E/BluetoothGatt(26644): Failed to register callback
03-27 11:33:49.840: I/BluetoothGattActivity(26644): myBluetoothGatt is null
누군가 내 문제가 무엇인지, 어떤 일이 벌어지고 있는지를 알면 도움을받을 수 있습니다. 나는 갤럭시 s4를 사용하고있다. 내 관련 코드가 모두 아래에 있다고 생각합니다.
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
private void fish(final BluetoothDevice device) {
Log.i(TAG, "In the fish function.");
myBluetoothGatt = device.connectGatt(this, false, myGattCallback);
Log.i(TAG, "myBluetoothGatt is " + myBluetoothGatt);
}
private BluetoothAdapter.LeScanCallback callbackFunction = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
if (DEVICE_NAME.equals(device.getName())) {
Log.i(TAG, "New LE Device matches !!!");
fish (device);
}
}
};
private final BluetoothGattCallback myGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
Log.i(TAG, "We are in the ConnectionStateChanged().");
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" +
myBluetoothGatt.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) {
Log.i(TAG, "In area 1");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
System.out.println("--onServicesDiscovered");
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status) {
Log.i(TAG, "In area 2");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE);
Log.i(TAG, "--onCharacteristicRead GATT_SUCCESS");
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "In area 3");
broadcastUpdate(ACTION_DATA_AVAILABLE);
System.out.println("--onCharacteristicChanged");
}
};