android - BLE connectGatt() 方法未在 BluetoothGattCallback 中报告任何内容....?

标签 android bluetooth-lowenergy android-bluetooth

有人可以检查这段代码的问题吗?我已经实现了一项服务并将 BluetoothDevice 对象传递给它(BluetoothDevice 对象通过 intent 传递没有错误/问题)。然后,在 onStartCommand() 中,我调用 deviceToConnect.connectGatt(this,false,mGattCallback)。但是我的 BluetoothGattCallback() 不工作(不打印任何东西)。代码简单明了。有人可以帮我调试它吗?

编辑:我在 MainActivity() 中执行 Le device Scan 并将设备对象传递给服务以连接到设备。

public class PairedBleService extends Service
{
    private BluetoothGatt mConnectedGatt;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);

        BluetoothDevice deviceToConnect = (BluetoothDevice) intent.getParcelableExtra(DEVICE_TO_CONNECT);
        mConnectedGatt = deviceToConnect.connectGatt(this, false, mGattCallback);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service End", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral connected", Toast.LENGTH_LONG).show();
        } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Toast.makeText(getApplicationContext(), "Peripheral Disconnected", Toast.LENGTH_LONG).show();
        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            gatt.disconnect();
        }
    }
}

编辑:我尝试将我的信标(外围设备)与标准的安卓蓝牙软件连接起来,然后我就可以建立连接了。但它要求配对引脚,一旦放置引脚,它就会连接并显示在已配对的蓝牙设备中。有没有像connectGatt这样的方法,我们可以在其中向用户询问“配对密码”……我无法理解我错过了什么。

最佳答案

您的 onBind 方法返回 null,因此您的主要 Activity 无法与您的服务通信。

您的代码应如下所示,

@PairedBleService

public class LocalBinder extends Binder 
{
    PairedBleService getService() 
    {
        return PairedBleService.this;
    };
};

@Override
public IBinder onBind(Intent intent) 
{
    // TODO Auto-generated method stub
    return mBinder;
};

private final IBinder mBinder = new LocalBinder();

@主要 Activity

//put this inside onServiceConnected

PairedBleService bleService = ((PairedBleService.LocalBinder) service).getService();

// Your code for connecting with BLE device
....................................
....................................

关于android - BLE connectGatt() 方法未在 BluetoothGattCallback 中报告任何内容....?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316088/

相关文章:

android - Android 地理围栏可以在没有网络连接且仅打开 GPS 的情况下工作吗?

android - 如何在 MultiSelection Spinner Android 中超过限制后禁用项目选择

Android USB 反向网络共享 : How to fool the apps

arduino - ESP32 Arduino BLE 深度 sleep

Android:强制蓝牙

java - Android 蓝牙 ScanFilter 部分字符串匹配

android - BluetoothGatt 连接后返回空服务列表

android - `theme`中的 `context.getColor`有什么用?

android - 解析信标的 URL

ios - 使用一个 CBManager 连接和写入多个外设