Bluetooth line in android

Bluetooth overview

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The app framework provides access to the Bluetooth functionality through Bluetooth APIs. These APIs let apps connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Using the Bluetooth APIs, an app can perform the following:

  • Scan for other Bluetooth devices.
  • Query the local Bluetooth adapter for paired Bluetooth devices.
  • Establish RFCOMM channels.
  • Connect to other devices through service discovery.
  • Transfer data to and from other devices.
  • Manage multiple connections.

This topic focuses on Classic Bluetooth. Classic Bluetooth is the right choice for more battery-intensive operations, which include streaming and communicating between devices. For Bluetooth devices with low power requirements, consider using Bluetooth Low Energy connections.

This documentation describes different Bluetooth profiles and explains how to use the Bluetooth APIs to accomplish the four major tasks necessary to communicate using Bluetooth:

  • Setting up Bluetooth.
  • Finding devices that are either paired or available in the local area.
  • Connecting devices.
  • Transferring data between devices.

For a demonstration of using the Bluetooth APIs, see the Bluetooth Chat sample app.

The basics

For Bluetooth-enabled devices to transmit data between each other, they must first form a channel of communication using a pairing process. One device, a discoverable device, makes itself available for incoming connection requests. Another device finds the discoverable device using a service discovery process. After the discoverable device accepts the pairing request, the two devices complete a bonding process in which they exchange security keys. The devices cache these keys for later use. After the pairing and bonding processes are complete, the two devices exchange information. When the session is complete, the device that initiated the pairing request releases the channel that had linked it to the discoverable device. The two devices remain bonded, however, so they can reconnect automatically during a future session as long as they’re in range of each other and neither device has removed the bond.

Читайте также:  Два наушника одновременно подключить bluetooth

Use of the Bluetooth APIs requires declaring several permissions in your manifest file. Once your app has permission to use Bluetooth, your app needs to access the BluetoothAdapter and determine if Bluetooth is available on the device. If Bluetooth is available, there are three steps to make a connection:

Certain devices use a specific Bluetooth profile that declares the data it provides.

Key classes and interfaces

All of the Bluetooth APIs are available in the android.bluetooth package. The following are the classes and interfaces you need in order to create Bluetooth connections:

BluetoothAdapter Represents the local Bluetooth adapter (Bluetooth radio). The BluetoothAdapter is the entry-point for all Bluetooth interaction. Using this, you can discover other Bluetooth devices, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for communications from other devices. BluetoothDevice Represents a remote Bluetooth device. Use this to request a connection with a remote device through a BluetoothSocket or query information about the device such as its name, address, class, and bonding state. BluetoothSocket Represents the interface for a Bluetooth socket (similar to a TCP Socket ). This is the connection point that allows an app to exchange data with another Bluetooth device using InputStream and OutputStream . BluetoothServerSocket Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket ). In order to connect two devices, one device must open a server socket with this class. When a remote Bluetooth device makes a connection request to this device, the device accepts the connection and then returns a connected BluetoothSocket . BluetoothClass Describes the general characteristics and capabilities of a Bluetooth device. This is a read-only set of properties that defines the device’s classes and services. Although this information provides a useful hint regarding a device’s type, the attributes of this class don’t necessarily describe all Bluetooth profiles and services that the device supports. BluetoothProfile An interface that represents a Bluetooth profile. A Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. An example is the Hands-Free profile. For more discussion of profiles, see Bluetooth profiles. BluetoothHeadset Provides support for Bluetooth headsets to be used with mobile phones. This includes both the Bluetooth Headset profile and the Hands-Free (v1.5) profile. BluetoothA2dp Defines how high-quality audio can be streamed from one device to another over a Bluetooth connection using the Advanced Audio Distribution Profile (A2DP). BluetoothHealth Represents a Health Device Profile proxy that controls the Bluetooth service. BluetoothHealthCallback An abstract class that you use to implement BluetoothHealth callbacks. You must extend this class and implement the callback methods to receive updates about changes in the app’s registration state and Bluetooth channel state. BluetoothHealthAppConfiguration Represents an app configuration that the Bluetooth Health third-party app registers to communicate with a remote Bluetooth health device. BluetoothProfile.ServiceListener An interface that notifies BluetoothProfile interprocess communication (IPC) clients when they have been connected to or disconnected from the internal service that runs a particular profile.

Читайте также:  Блютуз наушники работают поочередно

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2021-10-27 UTC.

Источник

android enable disable bluetooth via command line

I am trying to enable disable bluetooth on android device using command line. I can enable it using adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE But it prompts user to ‘allow’ or ‘deny’. I also see there is option to launch ble settings first like adb shell am start -a android.settings.BLUETOOTH_SETTINGS and then enable disable adb shell input keyevent ** But it won’t be device independent.

10 Answers 10

Updated: 2023-03-10 — Android 12/13

Enable bluetooth via cmd

adb shell cmd bluetooth_manager enable 

Disable bluetooth cmd

adb shell cmd bluetooth_manager disable 

Updated: 2019-06-22: — Android 11

adb shell settings get global bluetooth_on adb shell settings list global |grep ^bluetooth_on 

Enable Bluetooth via settings

adb shell settings put global bluetooth_disabled_profiles 1 

Disable bluetooth settings

adb shell settings put global bluetooth_disabled_profiles 0 

Enable bluetooth via content

adb shell content insert \ --uri content://settings/global \ --bind name:s:bluetooth_disabled_profiles \ --bind value:s:1 --user 0 

Disable bluetooth content

adb shell content insert \ --uri content://settings/global \ --bind name:s:bluetooth_disabled_profiles \ --bind value:s:0 --user 0 

Android 11/12/13 and older versions

Enable Bluetooth

adb shell settings put global bluetooth_on 1 

Disable Bluetooth

adb shell settings put global bluetooth_on 0 

Enable bluetooth via activity manager

adb shell am broadcast \ -a android.intent.action.BLUETOOTH_ENABLE --ez state true 

Disable bluetooth via activity manager

adb shell am broadcast \ -a android.intent.action.BLUETOOTH_ENABLE --ez state false 

Enable/Disable bluetooth via keyevents

adb shell am start \ -a android.settings.BLUETOOTH_SETTINGS \ adb shell input keyevent 19 adb shell input keyevent 23 

On Huawei P30 Pro with Android P even your new solution doesn’t work. When I use adb shell settings put global bluetooth_disabled_profiles 1 , it disables bluetooth (as opposite to what you say) and adb shell settings put global bluetooth_disabled_profiles 0 does nothing, it doesn’t enable nor disable it.

Читайте также:  Сажает ли блютуз батарею iphone

See if they have changed the name so it might be bluetooth_on instead of bluetooth_disabled_profiles. If you type ‘adb shell settings list global|grep -i bluetooth’ you should get some alternatives what you can try if above dosn’t work so just change name and value if needed, above works fine on Huawei P9, Samsung S8 and Huawei P8 that’s all I have tried on with Android 8.0 & Android 9.0. Regards

for me, all of these methods from 2019 edit (both disable and enable) disable bluetooth for just a very quick amount of time. It rather looks like a reset. Samsung A50, Android 10 (kernel 4.14 )

adb shell settings put global bluetooth_on 0 worked for me in android 12.1 — but is there anyway that I can make it non-persistent so that behavior is same after each boot? When settings put global bluetooth_on 0 — it retains the value and next time I reboot, I notice that boot_completed never gets set to 1. Unless I wipe /data partition and restart again and re-put settings put global bluetooth_on 0 .

adb shell service call bluetooth_manager 6 
adb shell service call bluetooth_manager 8 

We are trying to enable and disable Bluetooth before we start our automation test using adb commands Enable adb shell service call bluetooth_manager 6 Disable adb shell service call bluetooth_manager 9 We are getting the Bluetooth status using this command adb shell settings get global bluetooth_on Are there any other work around to enable and disable the bluetooth using APPIUM java because these commands working in some devices but not in all the device. Any solution for this? The same commands working in Android 5 (Lollipop)

using static method codes as suggested here will never work across all devices. These codes can and do change on a per device basis depending on vendor and OEM modifications.

Источник

Оцените статью
Adblock
detector