2

有没有办法强制开启蓝牙?

到目前为止,我发现的只是这个(使用我正在使用的 estimote sdk):

// Check if device supports Bluetooth Low Energy.
if (!beaconManager.hasBluetooth()) {
    Toast.makeText(this, "Device does not have Bluetooth Low Energy", Toast.LENGTH_LONG).show();
    return;
}

// If Bluetooth is not enabled, let user enable it.
if (!beaconManager.isBluetoothEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} 
else {
    connectToBlueTooth();
}

然后在onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == Activity.RESULT_OK) {
            connectToBlueTooth();
        }
        else {
            Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_LONG).show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

但这会询问用户是否要打开蓝牙......但是有没有办法在不询问用户的情况下打开它?

而且,如果没有办法做到这一点,我该如何在活动之外使用这种技术?

谢谢

4

1 回答 1

4

像这样尝试蓝牙适配器:

BluetoothAdapter.getDefaultAdapter().enable();
于 2014-04-10T10:39:25.080 回答