我的应用程序遇到了一些问题。我正在使用带有蓝牙的Texas设备,并且创建了一个简单的应用程序来通过蓝牙打开/关闭LED。如果我在接收器内部启用了与Texas和BLE扫描仪的应用程序配对的功能,则它们可以正常工作,如果在我的应用程序中使用配对功能,它将无法保存配对,并且无法正常工作。仅当接收方不强制配对时,我的APP才能工作。如何使配对保存在我的APP中?这是我的代码,您可以帮我吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
// Get UI elements
mTableDevices = (TableLayout) findViewById(R.id.devicesFound);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// For Android M: Check if app have location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
// Show explanation on why this is needed
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can discover bluetooth devices");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE);
}
});
builder.show();
} else {
// Prompt user for location access
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE);
}
}
}
// Configure scan filter on device name, so the scan result
// only displays devices with Project Zero running.
ScanFilter filter = new ScanFilter.Builder()
.setDeviceName("RCQ4-BLE")
.build();
mScanFilters.add(filter);
// Configure default scan settings
mScanSettings = new ScanSettings.Builder().build();
}