当前位置:   article > 正文

Android-系统服务-BluetoothManager

bluetoothmanager

0 快速索引表

  • 权限汇总

https://developer.android.google.cn/reference/android/Manifest.permission

/frameworks/base/core/res/AndroidManifest.xml

android.permission.BLUETOOTH

android.permission.BLUETOOTH_SCAN

android.permission.BLUETOOTH_CONNECT

android.permission.BLUETOOTH_ADVERTISE

android.permission.BLUETOOTH_ADMIN

android.permission.BLUETOOTH_PRIVILEGED

android.permission.BLUETOOTH_MAP

android.permission.BLUETOOTH_STACK

android.permission.LOCAL_MAC_ADDRESS

  • 接口汇总

https://developer.android.google.cn/reference/android/bluetooth/BluetoothManager

https://developer.android.google.cn/reference/android/bluetooth/BluetoothAdapter

https://developer.android.google.cn/reference/android/bluetooth/BluetoothSocket

/packages/modules/Bluetooth/framework/java/android/bluetooth/

/frameworks/base/core/java/android/bluetooth/

android.bluetooth.BluetoothManager.getAdapter

android.bluetooth.BluetoothAdapter.getDefaultAdapter

android.bluetooth.BluetoothAdapter.disable

android.bluetooth.BluetoothAdapter.enable

android.bluetooth.BluetoothAdapter.getAddress

android.bluetooth.BluetoothAdapter.isEnabled

android.bluetooth.BluetoothSocket.connect

android.bluetooth.BluetoothSocket.close

  • adb shell svc ~

打开蓝牙

  • adb shell svc bluetooth enable

关闭蓝牙

  • adb shell svc bluetooth disable
  • adb shell service call ~

打开蓝牙

  • adb shell service call bluetooth_manager 6

关闭蓝牙

  • adb shell service call bluetooth_manager 8 i32 1
  • adb shell settings

查询状态

  • adb shell settings get global bluetooth_on 0关1开 

修改状态

  • adb shell settings put global bluetooth_on 0/1
  • adb shell dumpsys ~

adb shell dumpsys bluetooth_manager

  • AppOps汇总

adb shell dumpsys appops --op 77

adb shell dumpsys appops --op 111

adb shell dumpsys appops --op 114


1 需求

  • 打开蓝牙的ADB命令
    • adb shell svc bluetooth enable
  • 关闭蓝牙的ADB命令
    • adb shell svc bluetooth disable
  • AppOps
    • adb shell dumpsys appops --op 77
    • adb shell dumpsys appops --op 111
    • adb shell dumpsys appops --op 114

2 权限

android.permission.BLUETOOTH

  • Added in API level 1
  • Protection level: normal
  1. 2207 <!-- Allows applications to connect to paired bluetooth devices.
  2. 2208 <p>Protection level: normal
  3. 2209 -->
  4. 2210 <permission android:name="android.permission.BLUETOOTH"
  5. 2211 android:description="@string/permdesc_bluetooth"
  6. 2212 android:label="@string/permlab_bluetooth"
  7. 2213 android:protectionLevel="normal" />

 android.permission.BLUETOOTH_SCAN

  • Added in API level 31
  • Protection level: dangerous
  1. 2215 <!-- Required to be able to discover and pair nearby Bluetooth devices.
  2. 2216 <p>Protection level: dangerous -->
  3. 2217 <permission android:name="android.permission.BLUETOOTH_SCAN"
  4. 2218 android:permissionGroup="android.permission-group.UNDEFINED"
  5. 2219 android:description="@string/permdesc_bluetooth_scan"
  6. 2220 android:label="@string/permlab_bluetooth_scan"
  7. 2221 android:protectionLevel="dangerous" />

 android.permission.BLUETOOTH_CONNECT

  • Added in API level 31
  • Protection level: dangerous
  1. 2223 <!-- Required to be able to connect to paired Bluetooth devices.
  2. 2224 <p>Protection level: dangerous -->
  3. 2225 <permission android:name="android.permission.BLUETOOTH_CONNECT"
  4. 2226 android:permissionGroup="android.permission-group.UNDEFINED"
  5. 2227 android:description="@string/permdesc_bluetooth_connect"
  6. 2228 android:label="@string/permlab_bluetooth_connect"
  7. 2229 android:protectionLevel="dangerous" />

 android.permission.BLUETOOTH_ADVERTISE

  • Added in API level 31
  • Protection level: dangerous
  1. 2231 <!-- Required to be able to advertise to nearby Bluetooth devices.
  2. 2232 <p>Protection level: dangerous -->
  3. 2233 <permission android:name="android.permission.BLUETOOTH_ADVERTISE"
  4. 2234 android:permissionGroup="android.permission-group.UNDEFINED"
  5. 2235 android:description="@string/permdesc_bluetooth_advertise"
  6. 2236 android:label="@string/permlab_bluetooth_advertise"
  7. 2237 android:protectionLevel="dangerous" />

android.permission.BLUETOOTH_ADMIN

  • Added in API level 1
  • Protection level: normal
  1. 2262 <!-- Allows applications to discover and pair bluetooth devices.
  2. 2263 <p>Protection level: normal
  3. 2264 -->
  4. 2265 <permission android:name="android.permission.BLUETOOTH_ADMIN"
  5. 2266 android:description="@string/permdesc_bluetoothAdmin"
  6. 2267 android:label="@string/permlab_bluetoothAdmin"
  7. 2268 android:protectionLevel="normal" />

android.permission.BLUETOOTH_PRIVILEGED

  • Added in API level 19
  • Not for use by third-party applications
  1. 2270 <!-- Allows applications to pair bluetooth devices without user interaction, and to
  2. 2271 allow or disallow phonebook access or message access.
  3. 2272 <p>Not for use by third-party applications. -->
  4. 2273 <permission android:name="android.permission.BLUETOOTH_PRIVILEGED"
  5. 2274 android:protectionLevel="signature|privileged" />

android.permission.BLUETOOTH_MAP

  1. 2276 <!-- @SystemApi Control access to email providers exclusively for Bluetooth
  2. 2277 @hide
  3. 2278 -->
  4. 2279 <permission android:name="android.permission.BLUETOOTH_MAP"
  5. 2280 android:protectionLevel="signature|role" />

android.permission.BLUETOOTH_STACK

  1. 2282 <!-- Allows bluetooth stack to access files
  2. 2283 @hide This should only be used by Bluetooth apk.
  3. 2284 -->
  4. 2285 <permission android:name="android.permission.BLUETOOTH_STACK"
  5. 2286 android:protectionLevel="signature|role" />

 

android.permission.LOCAL_MAC_ADDRESS

  1. 5844 <!-- @SystemApi Allows applications to read the local WiFi and Bluetooth MAC address.
  2. 5845 @hide -->
  3. 5846 <permission android:name="android.permission.LOCAL_MAC_ADDRESS"
  4. 5847 android:protectionLevel="signature|privileged" />
  5. 5848 <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>

3 接口

android.bluetooth.BluetoothManager.getAdapter

  • Added in API level 18
  1. 77 /**
  2. 78 * Get the BLUETOOTH Adapter for this device.
  3. 79 *
  4. 80 * @return the BLUETOOTH Adapter
  5. 81 */
  6. 82 @RequiresNoPermission
  7. 83 public BluetoothAdapter getAdapter() {
  8. 84 return mAdapter;
  9. 85 }

 

android.bluetooth.BluetoothAdapter.getDefaultAdapter

  • Added in API level 5
  • Deprecated in API level 31
  1. 947 /**
  2. 948 * Get a handle to the default local Bluetooth adapter.
  3. 949 * <p>
  4. 950 * Currently Android only supports one Bluetooth adapter, but the API could
  5. 951 * be extended to support more. This will always return the default adapter.
  6. 952 * </p>
  7. 953 *
  8. 954 * @return the default local adapter, or null if Bluetooth is not supported
  9. 955 * on this hardware platform
  10. 956 * @deprecated this method will continue to work, but developers are
  11. 957 * strongly encouraged to migrate to using
  12. 958 * {@link BluetoothManager#getAdapter()}, since that approach
  13. 959 * enables support for {@link Context#createAttributionContext}.
  14. 960 */
  15. 961 @Deprecated
  16. 962 @RequiresNoPermission
  17. 963 public static synchronized BluetoothAdapter getDefaultAdapter() {
  18. 964 if (sAdapter == null) {
  19. 965 sAdapter = createAdapter(AttributionSource.myAttributionSource());
  20. 966 }
  21. 967 return sAdapter;
  22. 968 }

android.bluetooth.BluetoothAdapter.disable

  • Added in API level 5
  • Deprecated in API level 33
  • 权限
  1. 1451 /**
  2. 1452 * Turn off the local Bluetooth adapter&mdash;do not use without explicit
  3. 1453 * user action to turn off Bluetooth.
  4. 1454 * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth
  5. 1455 * system services, and powers down the underlying Bluetooth hardware.
  6. 1456 * <p class="caution"><strong>Bluetooth should never be disabled without
  7. 1457 * direct user consent</strong>. The {@link #disable()} method is
  8. 1458 * provided only for applications that include a user interface for changing
  9. 1459 * system settings, such as a "power manager" app.</p>
  10. 1460 * <p>This is an asynchronous call: it will return immediately, and
  11. 1461 * clients should listen for {@link #ACTION_STATE_CHANGED}
  12. 1462 * to be notified of subsequent adapter state changes. If this call returns
  13. 1463 * true, then the adapter state will immediately transition from {@link
  14. 1464 * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time
  15. 1465 * later transition to either {@link #STATE_OFF} or {@link
  16. 1466 * #STATE_ON}. If this call returns false then there was an
  17. 1467 * immediate problem that will prevent the adapter from being turned off -
  18. 1468 * such as the adapter already being turned off.
  19. 1469 *
  20. 1470 * @return true to indicate adapter shutdown has begun, or false on immediate error
  21. 1471 *
  22. 1472 * @deprecated Starting with {@link android.os.Build.VERSION_CODES#TIRAMISU}, applications
  23. 1473 * are not allowed to enable/disable Bluetooth.
  24. 1474 * <b>Compatibility Note:</b> For applications targeting
  25. 1475 * {@link android.os.Build.VERSION_CODES#TIRAMISU} or above, this API will always fail and return
  26. 1476 * {@code false}. If apps are targeting an older SDK ({@link android.os.Build.VERSION_CODES#S}
  27. 1477 * or below), they can continue to use this API.
  28. 1478 * <p>
  29. 1479 * Deprecation Exemptions:
  30. 1480 * <ul>
  31. 1481 * <li>Device Owner (DO), Profile Owner (PO) and system apps.
  32. 1482 * </ul>
  33. 1483 */
  34. 1484 @Deprecated
  35. 1485 @RequiresLegacyBluetoothAdminPermission
  36. 1486 @RequiresBluetoothConnectPermission
  37. 1487 @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
  38. 1488 public boolean disable() {
  39. 1489 try {
  40. 1490 return mManagerService.disable(mAttributionSource, true);
  41. 1491 } catch (RemoteException e) {
  42. 1492 Log.e(TAG, "", e);
  43. 1493 }
  44. 1494 return false;
  45. 1495 }

android.bluetooth.BluetoothAdapter.enable

  • Added in API level 5
  • Deprecated in API level 33
  • 权限
    • android.permission.BLUETOOTH_ADMIN
    • android.permission.BLUETOOTH_CONNECT
  1. 1396 /**
  2. 1397 * Turn on the local Bluetooth adapter&mdash;do not use without explicit
  3. 1398 * user action to turn on Bluetooth.
  4. 1399 * <p>This powers on the underlying Bluetooth hardware, and starts all
  5. 1400 * Bluetooth system services.
  6. 1401 * <p class="caution"><strong>Bluetooth should never be enabled without
  7. 1402 * direct user consent</strong>. If you want to turn on Bluetooth in order
  8. 1403 * to create a wireless connection, you should use the {@link
  9. 1404 * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests
  10. 1405 * user permission to turn on Bluetooth. The {@link #enable()} method is
  11. 1406 * provided only for applications that include a user interface for changing
  12. 1407 * system settings, such as a "power manager" app.</p>
  13. 1408 * <p>This is an asynchronous call: it will return immediately, and
  14. 1409 * clients should listen for {@link #ACTION_STATE_CHANGED}
  15. 1410 * to be notified of subsequent adapter state changes. If this call returns
  16. 1411 * true, then the adapter state will immediately transition from {@link
  17. 1412 * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time
  18. 1413 * later transition to either {@link #STATE_OFF} or {@link
  19. 1414 * #STATE_ON}. If this call returns false then there was an
  20. 1415 * immediate problem that will prevent the adapter from being turned on -
  21. 1416 * such as Airplane mode, or the adapter is already turned on.
  22. 1417 *
  23. 1418 * @return true to indicate adapter startup has begun, or false on immediate error
  24. 1419 *
  25. 1420 * @deprecated Starting with {@link android.os.Build.VERSION_CODES#TIRAMISU}, applications
  26. 1421 * are not allowed to enable/disable Bluetooth.
  27. 1422 * <b>Compatibility Note:</b> For applications targeting
  28. 1423 * {@link android.os.Build.VERSION_CODES#TIRAMISU} or above, this API will always fail and return
  29. 1424 * {@code false}. If apps are targeting an older SDK ({@link android.os.Build.VERSION_CODES#S}
  30. 1425 * or below), they can continue to use this API.
  31. 1426 * <p>
  32. 1427 * Deprecation Exemptions:
  33. 1428 * <ul>
  34. 1429 * <li>Device Owner (DO), Profile Owner (PO) and system apps.
  35. 1430 * </ul>
  36. 1431 */
  37. 1432 @Deprecated
  38. 1433 @RequiresLegacyBluetoothAdminPermission
  39. 1434 @RequiresBluetoothConnectPermission
  40. 1435 @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
  41. 1436 public boolean enable() {
  42. 1437 if (isEnabled()) {
  43. 1438 if (DBG) {
  44. 1439 Log.d(TAG, "enable(): BT already enabled!");
  45. 1440 }
  46. 1441 return true;
  47. 1442 }
  48. 1443 try {
  49. 1444 return mManagerService.enable(mAttributionSource);
  50. 1445 } catch (RemoteException e) {
  51. 1446 Log.e(TAG, "", e);
  52. 1447 }
  53. 1448 return false;
  54. 1449 }


 

android.bluetooth.BluetoothAdapter.getAddress

  • Added in API level 5
  1. 1521 /**
  2. 1522 * Returns the hardware address of the local Bluetooth adapter.
  3. 1523 * <p>For example, "00:11:22:AA:BB:CC".
  4. 1524 *
  5. 1525 * @return Bluetooth hardware address as string
  6. 1526 *
  7. 1527 * Requires {@code android.Manifest.permission#LOCAL_MAC_ADDRESS} and
  8. 1528 * {@link android.Manifest.permission#BLUETOOTH_CONNECT}.
  9. 1529 */
  10. 1530 @RequiresLegacyBluetoothPermission
  11. 1531 @RequiresBluetoothConnectPermission
  12. 1532 public String getAddress() {
  13. 1533 try {
  14. 1534 return mManagerService.getAddress(mAttributionSource);
  15. 1535 } catch (RemoteException e) {
  16. 1536 Log.e(TAG, "", e);
  17. 1537 }
  18. 1538 return null;
  19. 1539 }

android.bluetooth.BluetoothAdapter.isEnabled

  • Added in API level 5 
  1. 1133 /**
  2. 1134 * Return true if Bluetooth is currently enabled and ready for use.
  3. 1135 * <p>Equivalent to:
  4. 1136 * <code>getBluetoothState() == STATE_ON</code>
  5. 1137 *
  6. 1138 * @return true if the local adapter is turned on
  7. 1139 */
  8. 1140 @RequiresLegacyBluetoothPermission
  9. 1141 @RequiresNoPermission
  10. 1142 public boolean isEnabled() {
  11. 1143 return getState() == BluetoothAdapter.STATE_ON;
  12. 1144 }

参考资料:

https://developer.android.google.cn/reference/android/bluetooth/BluetoothAdapter

/packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothAdapter.java

/frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java

android.bluetooth.BluetoothSocket.connect

  • Added in API level 5
  1. 402 /**
  2. 403 * Attempt to connect to a remote device.
  3. 404 * <p>This method will block until a connection is made or the connection
  4. 405 * fails. If this method returns without an exception then this socket
  5. 406 * is now connected.
  6. 407 * <p>Creating new connections to
  7. 408 * remote Bluetooth devices should not be attempted while device discovery
  8. 409 * is in progress. Device discovery is a heavyweight procedure on the
  9. 410 * Bluetooth adapter and will significantly slow a device connection.
  10. 411 * Use {@link BluetoothAdapter#cancelDiscovery()} to cancel an ongoing
  11. 412 * discovery. Discovery is not managed by the Activity,
  12. 413 * but is run as a system service, so an application should always call
  13. 414 * {@link BluetoothAdapter#cancelDiscovery()} even if it
  14. 415 * did not directly request a discovery, just to be sure.
  15. 416 * <p>{@link #close} can be used to abort this call from another thread.
  16. 417 *
  17. 418 * @throws IOException on error, for example connection failure
  18. 419 */
  19. 420 @RequiresBluetoothConnectPermission
  20. 421 @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
  21. 422 public void connect() throws IOException {
  22. 423 if (mDevice == null) throw new IOException("Connect is called on null device");
  23. 424
  24. 425 try {
  25. 426 if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
  26. 427 IBluetooth bluetoothProxy =
  27. 428 BluetoothAdapter.getDefaultAdapter().getBluetoothService();
  28. 429 if (bluetoothProxy == null) throw new IOException("Bluetooth is off");
  29. 430 IBluetoothSocketManager socketManager = bluetoothProxy.getSocketManager();
  30. 431 if (socketManager == null) throw new IOException("bt get socket manager failed");
  31. 432 mPfd = socketManager.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags());
  32. 433 synchronized (this) {
  33. 434 if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
  34. 435 if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
  35. 436 if (mPfd == null) throw new IOException("bt socket connect failed");
  36. 437 FileDescriptor fd = mPfd.getFileDescriptor();
  37. 438 mSocket = new LocalSocket(fd);
  38. 439 mSocketIS = mSocket.getInputStream();
  39. 440 mSocketOS = mSocket.getOutputStream();
  40. 441 }
  41. 442 int channel = readInt(mSocketIS);
  42. 443 if (channel <= 0) {
  43. 444 throw new IOException("bt socket connect failed");
  44. 445 }
  45. 446 mPort = channel;
  46. 447 waitSocketSignal(mSocketIS);
  47. 448 synchronized (this) {
  48. 449 if (mSocketState == SocketState.CLOSED) {
  49. 450 throw new IOException("bt socket closed");
  50. 451 }
  51. 452 mSocketState = SocketState.CONNECTED;
  52. 453 }
  53. 454 } catch (RemoteException e) {
  54. 455 Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
  55. 456 throw new IOException("unable to send RPC: " + e.getMessage());
  56. 457 }
  57. 458 }

android.bluetooth.BluetoothSocket.close

  • Added in API level 5
  1. 636 @Override
  2. 637 public void close() throws IOException {
  3. 638 Log.d(TAG, "close() this: " + this + ", channel: " + mPort + ", mSocketIS: " + mSocketIS
  4. 639 + ", mSocketOS: " + mSocketOS + ", mSocket: " + mSocket + ", mSocketState: "
  5. 640 + mSocketState);
  6. 641 if (mSocketState == SocketState.CLOSED) {
  7. 642 return;
  8. 643 } else {
  9. 644 synchronized (this) {
  10. 645 if (mSocketState == SocketState.CLOSED) {
  11. 646 return;
  12. 647 }
  13. 648 mSocketState = SocketState.CLOSED;
  14. 649 if (mSocket != null) {
  15. 650 if (DBG) Log.d(TAG, "Closing mSocket: " + mSocket);
  16. 651 mSocket.shutdownInput();
  17. 652 mSocket.shutdownOutput();
  18. 653 mSocket.close();
  19. 654 mSocket = null;
  20. 655 }
  21. 656 if (mPfd != null) {
  22. 657 mPfd.close();
  23. 658 mPfd = null;
  24. 659 }
  25. 660 }
  26. 661 }
  27. 662 }

参考资料

https://developer.android.google.cn/reference/android/bluetooth/BluetoothSocket

/packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothSocket.java

/frameworks/base/core/java/android/bluetooth/BluetoothSocket.java


4 示例

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.dsl.bluetoothdemo">
  4. <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  5. <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/Theme.BluetoothDemo">
  13. <activity
  14. android:name=".MainActivity"
  15. android:exported="true">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/btnBtstate"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="获取蓝牙状态" />
  13. <TextView
  14. android:id="@+id/tvBtState"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content" />
  17. <Button
  18. android:id="@+id/btnTurnOn"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="打开蓝牙 方法:enable"
  22. android:textAllCaps="false" />
  23. <Button
  24. android:id="@+id/btnTurnOnUseIntent"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="打开蓝牙 方法:intent"
  28. android:textAllCaps="false" />
  29. <Button
  30. android:id="@+id/btnTurnOff"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:text="关闭蓝牙"
  34. android:textAllCaps="false" />
  35. <Button
  36. android:id="@+id/btnGetAddress"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:text="获取蓝牙MAC地址" />
  40. <TextView
  41. android:id="@+id/tvBtAddress"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content" />
  44. </LinearLayout>

Main.java

  1. package com.dsl.bluetoothdemo;
  2. import android.bluetooth.BluetoothAdapter;
  3. import android.bluetooth.BluetoothManager;
  4. import android.content.Intent;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. import androidx.annotation.RequiresApi;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. /**
  13. * 第一步,判断设备是否支持蓝牙;
  14. * 第二步,判断设备蓝牙是否开启;
  15. * 第三步,操作蓝牙;
  16. */
  17. public class MainActivity extends AppCompatActivity {
  18. BluetoothManager bluetoothManager;
  19. BluetoothAdapter bluetoothAdapter;
  20. @RequiresApi(api = Build.VERSION_CODES.M)
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. Button btnBtState = findViewById(R.id.btnBtstate);
  26. TextView tvBtState = findViewById(R.id.tvBtState);
  27. Button btnTurnOn = findViewById(R.id.btnTurnOn);
  28. Button btnTurnOnUseIntent = findViewById(R.id.btnTurnOnUseIntent);
  29. Button btnTurnOff = findViewById(R.id.btnTurnOff);
  30. Button btnGetAddress = findViewById(R.id.btnGetAddress);
  31. TextView tvBtAddress = findViewById(R.id.tvBtAddress);
  32. /**
  33. * 获取蓝牙适配器实例,有两种方式,推荐方式二
  34. * 方式一:getSystemService
  35. * 方式二:getDefaultAdapter
  36. */
  37. // bluetoothManager = (BluetoothManager) getSystemService(BluetoothManager.class);
  38. bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
  39. bluetoothAdapter = bluetoothManager.getAdapter();
  40. // bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  41. btnBtState.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View view) {
  44. boolean state = bluetoothAdapter.isEnabled();
  45. tvBtState.setText(state + "");
  46. }
  47. });
  48. btnTurnOn.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View view) {
  51. bluetoothAdapter.enable();
  52. }
  53. });
  54. btnTurnOnUseIntent.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View view) {
  57. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  58. startActivityForResult(intent, 1);
  59. }
  60. });
  61. btnTurnOff.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View view) {
  64. bluetoothAdapter.disable();
  65. }
  66. });
  67. btnGetAddress.setOnClickListener(new View.OnClickListener() {
  68. @Override
  69. public void onClick(View view) {
  70. String mac = bluetoothAdapter.getAddress();
  71. tvBtAddress.setText(mac);
  72. }
  73. });
  74. }
  75. }


5 adb svc命令

adb shell svc bluetooth enable

adb shell svc bluetooth disable


6 adb appops命令 

adb shell dumpsys appops --op 77

  • APP_OP_BLUETOOTH_SCAN = 77
  • Manifest.permission.BLUETOOTH_SCAN

adb shell dumpsys appops --op 111

  •  APP_OP_BLUETOOTH_CONNECT = 111
  • Manifest.permission.BLUETOOTH_CONNECT

adb shell dumpsys appops --op 114

  • APP_OP_BLUETOOTH_ADVERTISE = 114
  • Manifest.permission.BLUETOOTH_ADVERTISE

参考资料: 

Bluetooth permissions  |  Android Developers

AppOpsManager.java - OpenGrok cross reference for /frameworks/base/core/java/android/app/AppOpsManager.javaenums.proto - OpenGrok cross reference for /frameworks/proto_logging/stats/enums/app/enums.proto


7 参考资料(权限)

Manifest.permission  |  Android Developers

/frameworks/base/core/res/AndroidManifest.xml


参考资料(接口)

https://developer.android.google.cn/reference/android/bluetooth/BluetoothManager

https://developer.android.google.cn/reference/android/bluetooth/BluetoothAdapter

https://developer.android.google.cn/reference/android/bluetooth/BluetoothSocket

/packages/modules/Bluetooth/framework/java/android/bluetooth/

/frameworks/base/core/java/android/bluetooth/


参考资料(指南)

蓝牙概览  |  Android 开发者  |  Android Developers

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/955259
推荐阅读
相关标签
  

闽ICP备14008679号