Following are code excerpts from android documentation on Bluetooth
In the manifest file for permissions:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
Source code to enable Bluetooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
If enabling Bluetooth succeeds, your Activity
will receive the RESULT_OK
result code in the onActivityResult()
callback. If Bluetooth was not enabled due to an error (or the user responded "No") then the result code will be RESULT_CANCELED
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…