4、遇到的问题

问题一

安装了Android Studio,第一次新建项目加载的时候出现了 "read timed out" 读取超时的问题

发现是一个jna 5.6.0.jar 下载失败,无法获得资源

能够下载,但是下载完后会重复下载,最后 "read timed out" ,读取超时

然后在网上查询发现可以手动下载,但是下载后解压文件放到目标目录下还是不行

最后发现要将解压包也放在目标文件

问题二

下列几行代码编写中会报错,但是运行中却没有报错

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

adapterPairedDevices.add(device.getName() + "\n" + device.getAddress());

if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
    adapterNearbyDevices.add(device.getName() + "\n" + device.getAddress());
}

//          判断当前是否正在查找设备,是返回true
        if (bluetoothAdapter.isDiscovering()) {
//          取消查找设备
            bluetoothAdapter.cancelDiscovery();
        }
//      开始查找设备
        bluetoothAdapter.startDiscovery();

为了消除错误需要选择下图中的”Add permission check“

 出现下面的代码,但是添加这段代码后,应用会有异常——蓝牙扫描不出设备 

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}

问题三

不触发onCharacteristicChanged的回调

添加下面的代码就能够触发 

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (bluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "适配器未初始化");
        return;
    }
    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    bluetoothGatt.writeDescriptor(descriptor);
}

参考链接:Android 蓝牙BLE 中onCharacteristicChanged不走回调的解决方案_android ble writedescriptor设置成功没有回调_ITPlus的博客-CSDN博客

 

问题四

在添加页面添加一条item同时在数据库中插入一条数据,再返回列表页面后,查询数据库时会发生错误。错误异常:java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabas

发现是因为Activity的生命周期问题,之前是将数据库的打开和关闭放在onStart和onStop中。

已解决:将数据库的打开和关闭放在onResume和onPause中

 

问题五

发现在子线程中使用Tosat会报错Can't toast on a thread that has not called Looper.prepare()

解决方法:

1、在Tosat前后添加Looper.prepare();和Looper.loop();

2、在子线程中使用runOnUiThread(); 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值