2022年2月9日 星期三

Android Studio - BluetoothAdapter Class(藍牙適配器類)(三十五)

Android Studio - BluetoothAdapter Class(藍牙適配器類)(三十五):

Android 的本地設備 BluetoothAdapter(藍牙適配器)。BluetoothAdapter 允許執行基本的藍牙任務,例如啟動設備發現、查詢綁定(配對)設備的列表、使用已知 MAC 地址實例化 BluetoothDevice,創建 BluetoothServerSocket 以偵聽來自其他設備的連接請求,並啟動一個掃描藍牙設備。

Android Studio BluetoothAdapter Class(藍牙適配器類)
BluetoothAdapter(藍牙適配器):

Class 屬性

參數

描述

ACTION_CONNECTION_STATE_CHANGED

String

Intent used to broadcast the change in connection state of the local Bluetooth adapter to a profile of the remote device.

ACTION_DISCOVERY_FINISHED

String

Broadcast Action: The local Bluetooth adapter has finished the device discovery process.

ACTION_DISCOVERY_STARTED

String

Broadcast Action: The local Bluetooth adapter has started the remote device discovery process.

ACTION_LOCAL_NAME_CHANGED

String

Broadcast Action: The local Bluetooth adapter has changed its friendly Bluetooth name.

ACTION_REQUEST_DISCOVERABLE

String

Activity Action: Show a system activity that requests discoverable mode.

ACTION_REQUEST_ENABLE

String

Activity Action: Show a system activity that allows the user to turn on Bluetooth.

ACTION_SCAN_MODE_CHANGED

String

Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter has changed.

ACTION_STATE_CHANGED

String

Broadcast Action: The state of the local Bluetooth adapter has been changed.

ERROR

int

Sentinel error value for this class.

EXTRA_CONNECTION_STATE

String

Extra used by ACTION_CONNECTION_STATE_CHANGED This extra represents the current connection state.

EXTRA_DISCOVERABLE_DURATION

String

Used as an optional int extra field in ACTION_REQUEST_DISCOVERABLE intents to request a specific duration for discoverability in seconds.

EXTRA_LOCAL_NAME

String

Used as a String extra field in ACTION_LOCAL_NAME_CHANGED intents to request the local Bluetooth name.

EXTRA_PREVIOUS_CONNECTION_STATE

String

Extra used by ACTION_CONNECTION_STATE_CHANGED This extra represents the previous connection state.

EXTRA_PREVIOUS_SCAN_MODE

String

Used as an int extra field in ACTION_SCAN_MODE_CHANGED intents to request the previous scan mode.

EXTRA_PREVIOUS_STATE

String

Used as an int extra field in ACTION_STATE_CHANGED intents to request the previous power state.

EXTRA_SCAN_MODE

String

Used as an int extra field in ACTION_SCAN_MODE_CHANGED intents to request the current scan mode.

EXTRA_STATE

String

Used as an int extra field in ACTION_STATE_CHANGED intents to request the current power state.

SCAN_MODE_CONNECTABLE

int

Indicates that inquiry scan is disabledbut page scan is enabled on the local Bluetooth adapter.

SCAN_MODE_CONNECTABLE_DISCOVERABLE

int

Indicates that both inquiry scan and page scan are enabled on the local Bluetooth adapter.

SCAN_MODE_NONE

int

Indicates that both inquiry scan and page scan are disabled on the local Bluetooth adapter.

STATE_CONNECTED

int

The profile is in connected state

STATE_CONNECTING

int

The profile is in connecting state

STATE_DISCONNECTED

int

The profile is in disconnected state

STATE_DISCONNECTING

int

The profile is in disconnecting state

STATE_OFF

int

Indicates the local Bluetooth adapter is off.

STATE_ON

int

Indicates the local Bluetooth adapter is onand ready for use.

STATE_TURNING_OFF

int

Indicates the local Bluetooth adapter is turning off.

STATE_TURNING_ON

int

Indicates the local Bluetooth adapter is turning on.


BluetoothAdapter(藍牙適配器)Public Methods:

Class 屬性

參數

描述

cancelDiscovery()

boolean

Cancel the current device discovery process.

checkBluetoothAddress(String address)

static boolean

Validate a String Bluetooth addresssuch as "00:43:A8:23:10:F0"

 

Alphabetic characters must be uppercase to be valid.

closeProfileProxy(int profile,BluetoothProfile proxy)

void

Close the connection of the profile proxy to the Service.

disable()

boolean

Turn off the local Bluetooth adapter—do not use without explicit user action to turn off Bluetooth.

enable()

boolean

Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.

getAddress()

String

Returns the hardware address of the local Bluetooth adapter.

getBluetoothLeAdvertiser()

BluetoothLeAdvertiser

Returns a BluetoothLeAdvertiser object for Bluetooth LE Advertising operations.

getBluetoothLeScanner()

BluetoothLeScanner

Returns a BluetoothLeScanner object for Bluetooth LE scan operations.

getBondedDevices()

Set<BluetoothDevice>

Return the set of BluetoothDevice objects that are bonded (paired) to the local adapter.

getDefaultAdapter()

static BluetoothAdapter

This method was deprecated in API level 31this method will continue to workbut developers are strongly encouraged to migrate to using BluetoothManager#getAdapter()since that approach enables support for Context#createAttributionContext.

getLeMaximumAdvertisingDataLength()

int

Return the maximum LE advertising data length in bytesif LE Extended Advertising feature is supported0 otherwise.

getName()

String

Get the friendly Bluetooth name of the local Bluetooth adapter.

getProfileConnectionState(int profile)

int

Get the current connection state of a profile.

getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)

boolean

Get the profile proxy object associated with the profile.

getRemoteDevice(byte[] address)

BluetoothDevice

Get a BluetoothDevice object for the given Bluetooth hardware address.

getRemoteDevice(String address)

BluetoothDevice

Get a BluetoothDevice object for the given Bluetooth hardware address.

getScanMode()

int

Get the current Bluetooth scan mode of the local Bluetooth adapter.

getState()

int

Get the current state of the local Bluetooth adapter.

isDiscovering()

boolean

Return true if the local Bluetooth adapter is currently in the device discovery process.

isEnabled()

boolean

Return true if Bluetooth is currently enabled and ready for use.

isLe2MPhySupported()

boolean

Return true if LE 2M PHY feature is supported.

isLeCodedPhySupported()

boolean

Return true if LE Coded PHY feature is supported.

isLeExtendedAdvertisingSupported()

boolean

Return true if LE Extended Advertising feature is supported.

isLePeriodicAdvertisingSupported()

boolean

Return true if LE Periodic Advertising feature is supported.

isMultipleAdvertisementSupported()

boolean

Return true if the multi advertisement is supported by the chipset

For apps targeting Build.VERSION_CODES#R or lowerthis requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.

isOffloadedFilteringSupported()

boolean

Return true if offloaded filters are supported

For apps targeting Build.VERSION_CODES#R or lowerthis requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.

isOffloadedScanBatchingSupported()

boolean

Return true if offloaded scan batching is supported

For apps targeting Build.VERSION_CODES#R or lowerthis requires the Manifest.permission#BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.

listenUsingInsecureL2capChannel()

BluetoothServerSocket

Create an insecure L2CAP Connection-oriented Channel (CoC) BluetoothServerSocket and assign a dynamic PSM value.

listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)

BluetoothServerSocket

Create a listeninginsecure RFCOMM Bluetooth socket with Service Record.

listenUsingL2capChannel()

BluetoothServerSocket

Create a secure L2CAP Connection-oriented Channel (CoC) BluetoothServerSocket and assign a dynamic protocol/service multiplexer (PSM) value.

listenUsingRfcommWithServiceRecord(String name, UUID uuid)

BluetoothServerSocket

Create a listeningsecure RFCOMM Bluetooth socket with Service Record.

setName(String name)

boolean

Set the friendly Bluetooth name of the local Bluetooth adapter.

startDiscovery()

boolean

Start the remote device discovery process.

startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)

boolean

This method was deprecated in API level 21use BluetoothLeScanner#startScan(ListScanSettingsScanCallback) instead.

startLeScan(BluetoothAdapter.LeScanCallback callback)

boolean

This method was deprecated in API level 21use BluetoothLeScanner#startScan(ListScanSettingsScanCallback) instead.

stopLeScan(BluetoothAdapter.LeScanCallback callback)

void

This method was deprecated in API level 21Use BluetoothLeScanner#stopScan(ScanCallback) instead.


啟用藍牙設備範例:

if (!mBluetoothAdapter.isEnabled()) {

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}


參考網址:
※ BluetoothAdapter

2022年 2月 9日(Wed)天氣報告
氣溫:43.0°F / 6.0°C @ 07:00
風速:每小時 2公里
降雨機會:30%
相對濕度:百分之 88%
天氣:多雲

沒有留言:

張貼留言