繁体   English   中英

Android应用未连接到低功耗蓝牙设备

[英]Android app doesn't connect to a Bluetooth Low Energy device

我从Android开发开始,正在尝试在我的android手机和微控制器(PSoC4BLE)之间建立简单的蓝牙低能耗连接,以在微控制器特征之一中编写一个值。

正如我已经知道微控制器MAC以及服务和特有的UUID一样,我只想在我的android应用打开后立即与微控制器建立连接,而无需任何用户交互,并且当我按下按钮时,该应用会写将值转化为特征。

问题是我的应用程序在运行时崩溃,在调整代码的情况下我无法正常工作,但它没有连接到微控制器,我在做什么错呢?

这是代码:

public class MainActivity extends Activity {

// bluetooth variables
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattCharacteristic mMyCharacteristic;
private static final UUID CAR_SERVICE = UUID.fromString("00000000-0000-1000-1000-100000000000");
private static final UUID CAR_CHARACTERISTIC = UUID.fromString("00000000-0000-2000-2000-200000000000");
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // initialize bluetooth adapter
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // create a device that represents the bluetooth device and assign it's known MAC address
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:A0:50:0F:13:1C");

    // make the connection to the device
    mBluetoothGatt = device.connectGatt(MainActivity.this, false, mGattCallback);

    // get a reference to my user interface button
    Button btnWrite = (Button)findViewById(R.id.BtnWrite);

    // define the button behaviour
    btnWrite.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // create the characteristic variable
            byte[] value = new byte[1];
            value[0] = (byte) 1;
            mMyCharacteristic.setValue(value);
            // set the service UUID and the characteristic UUID
            mMyCharacteristic = mBluetoothGatt.getService(CAR_SERVICE).getCharacteristic(CAR_CHARACTERISTIC);
            // write the characteristic en the device
            mBluetoothGatt.writeCharacteristic(mMyCharacteristic);
        }
    });
}

}

更新1最后,我得到一个logcat,它说:

java.lang.RuntimeException:无法启动活动ComponentInfo {lenovo.car/lenovo.car.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java。 lang.String)'在空对象引用上引起的原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)'

拜托,有什么主意吗? 我发现该应用程序在final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:A0:50:0F:13:1C");崩溃了final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:A0:50:0F:13:1C");

您实际上没有连接到远程设备。

// make the connection to the device
mBluetoothGatt = device.connectGatt(MainActivity.this, false, mGattCallback);

获取BluetoothGatt实例还不够,您需要在Gatt对象上调用mBluetoohGatt.connect()方法。

还要注意, mBluetoohGatt.connect()返回布尔值,指示是否发送了连接请求。

此外,下一步应更改连接状态。 这是通过通知的

mGattCallback.onConnectionStateChange()方法。

建立连接后,您应该发送请求以获取设备服务。 可通过以下方式获得

mBluetoothGatt.discoverServices方法。

如果服务发现请求成功,您应该收到设备的所有服务。

然后,您可以获得设备的所有服务和特征。 此时,您可以假定您已连接到远程设备,但前提是该设备不需要任何身份验证。

为了读取特定特性下的当前值,您必须

调用mBluetoothGatt.readCharacteristic()应用要查找其值的特征。

插入值的特征应通过mGattCallback.onCharacteristicRead()进行通知。 这在具有您所要求的值的特征可读的条件下起作用。

为了在特定特性下写入当前值,您必须

调用mBluetoothGatt.writeCharacteristic()应用具有新值的特征。

作为对您请求的确认,您应该通过mBluetoothGatt.onCharacteristicWrite()收到通知。 如果您的特征可写,则此方法有效。

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM