TI BLE设备与自定义Android APP配对绑定故障求助
Hey there! Let's break down your BLE pairing/bonding issue step by step—since you're new to Android Studio and BLE development, we'll start with the most common pitfalls and work our way through fixes.
First, Let's Cover the Basics: Permissions
Android's BLE permissions got stricter starting from API 31 (Android 12), so make sure your app has all the necessary permissions set up correctly:
- Add these to your
AndroidManifest.xml:<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" /> - Don't forget to request these permissions dynamically in your activity (especially
BLUETOOTH_SCANandBLUETOOTH_CONNECTfor Android 12+). Missing permissions often cause silent failures with pairing/bonding.
Fix Your Bonding Trigger Logic
Your first code snippet calls createBond() directly in the scan callback—this is problematic because scan callbacks fire repeatedly every time the device is detected. This can lead to multiple overlapping bonding attempts, which will fail. Instead:
- Trigger bonding only after the user selects a device (e.g., in your
SelectedDeviceActivitywhen the user confirms their choice), not during scanning. - Example code for triggering bonding in
SelectedDeviceActivity:// Call this when the user selects the device (e.g., button click or onCreate) BluetoothDevice selectedDevice = getIntent().getParcelableExtra("SELECTED_DEVICE"); if (selectedDevice != null && selectedDevice.getBondState() != BluetoothDevice.BOND_BONDED) { boolean bondingInitiated = selectedDevice.createBond(); Log.d(TAG, "Bonding initiated: " + bondingInitiated); }
Expand Your Broadcast Receiver to Handle Pairing Requests
Your current receiver only tracks bond state changes, but it's missing handling for pairing requests. If your TI BLE device requires a passkey (common in secured pairing), Android will send a ACTION_PAIRING_REQUEST broadcast that your app needs to respond to—otherwise, the pairing will time out or fail.
Update your receiver like this:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); switch (bondState) { case BluetoothDevice.BOND_BONDING: Log.d(TAG, "Bonding in progress..."); break; case BluetoothDevice.BOND_BONDED: Log.d(TAG, "Successfully bonded! Now connect to BLE GATT."); // Initiate your BLE GATT connection here break; case BluetoothDevice.BOND_NONE: if (prevBondState == BluetoothDevice.BOND_BONDING) { Log.e(TAG, "Bonding failed! Check passkey or device settings."); // Add user feedback here (e.g., toast message) } break; } } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Replace "0000" with your TI device's fixed passkey (check TI firmware config) if (device != null) { try { // Set the passkey and cancel system default pairing popup boolean success = device.setPin("0000".getBytes()); abortBroadcast(); Log.d(TAG, "Passkey set successfully: " + success); } catch (Exception e) { e.printStackTrace(); } } } } };
And make sure you register the receiver with both actions:
IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST); registerReceiver(mReceiver, filter);
Check Your TI BLE Device's Firmware Configuration
TI BLE devices (like CC2640/CC2650) have configurable pairing settings that must match your app's behavior:
- Use TI tools like SmartRF Studio or BLE Scanner to check:
- Is bonding enabled on the device?
- What pairing mode is set? (e.g., fixed passkey, passkey entry, no passkey)
- Is MITM (Man-in-the-Middle) protection required? If yes, your app must handle passkey input.
- Ensure the passkey in your app matches the one configured on the TI device (common defaults are "0000" or "1234").
Debug with Logcat
Filter Logcat for Bluetooth-related tags (e.g., BluetoothAdapter, BluetoothGatt, your app's TAG) to see detailed error messages. Look for lines like:
Pairing rejectedAuthentication failedBond state changed: BOND_BONDING -> BOND_NONE
These logs will point you to exactly where the pairing/bonding is failing.
内容的提问来源于stack exchange,提问作者MarioCas

