我正在使用蓝牙LE。它找到的其他设备,但它无法连接到它并获取它发送设备的数据。
nameDeviceList.clear();
deviceList.clear();
listView = (ListView) popupView.findViewById(R.id.list_view_devices);
arrayAdapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, nameDeviceList);
listView.setAdapter(arrayAdapter);
BluetoothManager bluetoothManager =
(BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mHandler = new android.os.Handler();
scanLeDevice(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Application.writeFileSDBLT(deviceList.get(position).getName());
g = deviceList.get(position).connectGatt(gView.getContext(), false, mGattCallback);
// Application.writeFileSDBLT(g.getServices().get(0).getCharacteristics().get(0).getUuid().toString());
}
});
扫描设备的方法:
public void scanLeDevice(boolean enable) {
if (enable) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
/**
* Callback indicating when GATT client has connected/disconnected to/from a remote
* GATT server.
*
* @param gatt GATT client
* @param status Status of the connect or disconnect operation.
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
* @param newState Returns the new connection state. Can be one of
* {@link BluetoothProfile#STATE_DISCONNECTED} or
* {@link BluetoothProfile#STATE_CONNECTED}
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
Application.writeFileSDBLT(" BLT _Connected ");
g.discoverServices();
// onServicesDiscovered(gatt, status);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Application.writeFileSDBLT("DISCONNECTED BLT _Connected ");
}
}
/**
* Callback reporting the result of a characteristic read operation.
*
* @param gatt GATT client invoked {@link BluetoothGatt#readCharacteristic}
* @param characteristic Characteristic that was read from the associated
* remote device.
* @param status {@link BluetoothGatt#GATT_SUCCESS} if the read operation
*/
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
// super.onCharacteristicRead(gatt, characteristic, status);
String UUID_HEART_RATE_MEASUREMENT = SampleGattAttributes.HEART_RATE_MEASUREMENT;
if(status == BluetoothGatt.GATT_SUCCESS)
Application.writeFileSDBLT("GAAT Connection Success");
else
Application.writeFileSDBLT("GAAT Connection Failed");
}
/**
* Callback invoked when the list of remote services, characteristics and descriptors
* for the remote device have been updated, ie new services have been discovered.
*
* @param gatt GATT client invoked {@link BluetoothGatt#discoverServices}
* @param status {@link BluetoothGatt#GATT_SUCCESS} if the remote device
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//super.onServicesDiscovered(gatt, status);
if(status == BluetoothGatt.GATT_SUCCESS)
Application.writeFileSDBLT("GATT Connection SERVICESS :onServicesDiscovered + size services: "+gatt.getServices().size());
else
Application.writeFileSDBLT("GATT no Connection Services");
// Application.writeFileSDBLT("asdssssssssssssf-"+gatt.getServices().get(0).getCharacteristics().size());
}
};