我在项目中使用了 android.bluetooth 包,但我尝试实现 IBluetoothGatt 来实现读写特性。但我有一些问题,如下所示
public final class BluetoothGatt implements BluetoothProfile {
private static final String TAG = "BluetoothGatt";
private static final boolean DBG = true;
private static final boolean VDBG = false;
private IBluetoothGatt mService; // IBluetoothGatt red highlights. Some functions in IBluetoothGatt interface just work by put breakpoints.
private BluetoothGattCallback mCallback;
private int mClientIf;
private boolean mAuthRetry = false;
private BluetoothDevice mDevice;
private boolean mAutoConnect;
private int mConnState;
private final Object mStateLock = new Object();
private Boolean mDeviceBusy = false;
private int mTransport;
private static final int CONN_STATE_IDLE = 0;
private static final int CONN_STATE_CONNECTING = 1;
private static final int CONN_STATE_CONNECTED = 2;
private static final int CONN_STATE_DISCONNECTING = 3;
private static final int CONN_STATE_CLOSED = 4;
private List<BluetoothGattService> mServices;
在IBluetoothGatt界面中写入特征红色高亮
public void onCharacteristicWrite(String address, int status, int handle) {
if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address
+ " handle=" + handle + " Status=" + status);
if (!address.equals(mDevice.getAddress())) {
return;
}
synchronized(mDeviceBusy) {
mDeviceBusy = false;
}
BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle);
if (characteristic == null) return;
if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION)
&& mAuthRetry == false) {
try {
mAuthRetry = true;
mService.writeCharacteristic(mClientIf, address, handle,
characteristic.getWriteType(), AUTHENTICATION_MITM,
characteristic.getValue());
return;
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
}
使用这篇文章非常有用。 BLE
有任何问题请评论
这是连接 ble 设备的代码。
public boolean connect(final String address)
{
if (mBluetoothAdapter == null || address == null)
{
Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null)
{
Log.e(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
return true;
}
作为记录,IBluetoothGatt 源代码可以在以下位置找到: https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-5.0.2_r1/src/com/android/bluetooth/gatt/GattService .java#259
Android 使用称为 Binder 的系统(Android 特定的 IPC 模型)将用户应用程序(即您正在编写的应用程序)与实际实现功能的系统服务分开。对于大多数 Android 框架,代码是“直接”从您的应用程序链接的,无需 IPC 桥,这就是为什么您习惯于通过 IDE 轻松查找源代码。对于更高级的用例,显式进程分离很重要,它们使用 IPC 桥,并将隐藏从 aidl 文件创建的自动生成的 IPC 接口后面的功能(通常是 ISomethingSomething,您可以在主文件中找到 ISomethingSomething.aidl 文件)框架来源)。例如,IBluetoothGatt.aidl。
这些aidl文件的实现通常可以在frameworks/base/services或frameworks/base/opt目录之一中找到。 在蓝牙的情况下,它位于提供的packages目录之一。这里的差异归因于他们是否希望能够编写带有或不带有 APK 的 Android 版本。例如,如果他们想要发布不支持蓝牙的 Android 版本,他们只需省略蓝牙包 APK 即可。
浏览这些来源有时很重要,因为您会发现许多令人惊讶的错误或错误的文档,尤其是对于前沿 API。