㈠ android藍牙ble4.0開發共享失敗怎麼辦
2.1首先獲取BluetoothManager
復制代碼 代碼如下:
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
2.2獲取BluetoothAdapter
復制代碼 代碼如下:
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
2.3創建BluetoothAdapter.LeScanCallback
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String struuid = NumberUtils.bytes2HexString(NumberUtils.reverseBytes(scanRecord)).replace("-", "").toLowerCase();
if (device!=null && struuid.contains(DEVICE_UUID_PREFIX.toLowerCase())) {
mBluetoothDevices.add(device);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
2.4.開始搜索設備。
復制代碼 代碼如下:
mBluetoothAdapter.startLeScan(mLeScanCallback);
2.5.BluetoothDevice 描述了一個藍牙設備 提供了getAddress()設備Mac地址,getName()設備的名稱。
2.6開始連接設備
/**
* Connects to the GATT server hosted on the Bluetooth LE device.
*
* @param address
* The device address of the destination device.
*
* @return Return true if the connection is initiated successfully. The
* connection result is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
*/
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect. (先前連接的設備。 嘗試重新連接)
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the
// autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
2.7連接到設備之後獲取設備的服務(Service)和服務對應的Characteristic。
// Demonstrates how to iterate through the supported GATT
// Services/Characteristics.
// In this sample, we populate the data structure that is bound to the
// ExpandableListView
// on the UI.
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null)
return;
String uuid = null;
ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData = new ArrayList<>();
mGattCharacteristics = new ArrayList<>();
// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap<String, String> currentServiceData = new HashMap<>();
uuid = gattService.getUuid().toString();
if (uuid.contains("ba11f08c-5f14-0b0d-1080")) {//服務的uuid
//System.out.println("this gattService UUID is:" + gattService.getUuid().toString());
currentServiceData.put(LIST_NAME, "Service_OX100");
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);
ArrayList<HashMap<String, String>> gattCharacteristicGroupData = new ArrayList<>();
List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<>();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap<String, String> currentCharaData = new HashMap<>();
uuid = gattCharacteristic.getUuid().toString();
if (uuid.toLowerCase().contains("cd01")) {
currentCharaData.put(LIST_NAME, "cd01");
} else if (uuid.toLowerCase().contains("cd02")) {
currentCharaData.put(LIST_NAME, "cd02");
} else if (uuid.toLowerCase().contains("cd03")) {
currentCharaData.put(LIST_NAME, "cd03");
} else if (uuid.toLowerCase().contains("cd04")) {
currentCharaData.put(LIST_NAME, "cd04");
} else {
currentCharaData.put(LIST_NAME, "write");
}
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
}
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
mCharacteristicCD01 = gattService.getCharacteristic(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb"));
mCharacteristicCD02 = gattService.getCharacteristic(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb"));
mCharacteristicCD03 = gattService.getCharacteristic(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb"));
mCharacteristicCD04 = gattService.getCharacteristic(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb"));
mCharacteristicWrite = gattService.getCharacteristic(UUID.fromString("0000cd20-0000-1000-8000-00805f9b34fb"));
//System.out.println("=======================Set Notification==========================");
// 開始順序監聽,第一個:CD01
mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD01, true);
mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD02, true);
mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD03, true);
mBluetoothLeService.setCharacteristicNotification(mCharacteristicCD04, true);
}
}
}
2.8獲取到特徵之後,找到服務中可以向下位機寫指令的特徵,向該特徵寫入指令。
public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.writeCharacteristic(characteristic);
}
2.9寫入成功之後,開始讀取設備返回來的數據。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
//System.out.println("=======status:" + status);
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
//從特徵中讀取數據
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//System.out.println("onCharacteristicRead");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
//向特徵中寫入數據
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//System.out.println("--------write success----- status:" + status);
}
/*
* when connected successfully will callback this method this method can
* dealwith send password or data analyze
*當連接成功將回調該方法
*/
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
if (characteristic.getValue() != null) {
//System.out.println(characteristic.getStringValue(0));
}
//System.out.println("--------onCharacteristicChanged-----");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
//System.out.println(" = " + status + ", descriptor =" + descriptor.getUuid().toString());
UUID uuid = descriptor.getCharacteristic().getUuid();
if (uuid.equals(UUID.fromString("0000cd01-0000-1000-8000-00805f9b34fb"))) {
broadcastUpdate(ACTION_CD01NOTIDIED);
} else if (uuid.equals(UUID.fromString("0000cd02-0000-1000-8000-00805f9b34fb"))) {
broadcastUpdate(ACTION_CD02NOTIDIED);
} else if (uuid.equals(UUID.fromString("0000cd03-0000-1000-8000-00805f9b34fb"))) {
broadcastUpdate(ACTION_CD03NOTIDIED);
} else if (uuid.equals(UUID.fromString("0000cd04-0000-1000-8000-00805f9b34fb"))) {
broadcastUpdate(ACTION_CD04NOTIDIED);
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
//System.out.println("rssi = " + rssi);
}
};
----------------------------------------------
//從特徵中讀取數據
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//System.out.println("onCharacteristicRead");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
㈡ android ble 和ios ble的區別
iBeacon
在ios 中ibeacon是基於地理位置的微定位技術,雖然藉助手機藍牙進行接收Majro、Minor,但是在開發工程中沒有任何關系。
ios 在ble、ibeacon 開發過程中與Android 的區別
在ios 中所有的數據都是通過API獲取的,也就是說在IOS中不會看到藍牙模塊的裸數據,只能拿到蘋果公司提供的極個別的API中的數據。
ble、ibeacon各使用各自的API,之間沒有任何對應關系。如果想使用ble就不可能獲取到ibeacon的major、minor、uuid 等信息,如果使用ibeacon,沒有辦法發起鏈接請求獲取服務。
在ios中ibeacon通信數據只有六個屬性,就是蘋果提供的幾個表示距離的屬性,是一個float類型數據。
㈢ android ble搜到一個藍牙怎麼跟特徵值通信的
Generic Attribute Profile (GATT)
通過BLE連接,讀寫屬性類小數據的Profile通用規范。現在所有的BLE應用Profile都是基於GATT的。
Attribute Protocol (ATT)
GATT是基於ATT Protocol的。ATT針對BLE設備做了專門的優化,具體就是在傳輸過程中使用盡量少的數據。每個屬性都有一個唯一的UUID,屬性將以characteristics and services的形式傳輸。
Characteristic
Characteristic可以理解為一個數據類型,它包括一個value和0至多個對次value的描述(Descriptor)。
Descriptor
對Characteristic的描述,例如范圍、計量單位等。
Service
Characteristic的集合。例如一個service叫做「Heart Rate Monitor」,它可能包含多個Characteristics,其中可能包含一個叫做「heart rate measurement"的Characteristic。
二、角色和職責:
Android設備與BLE設備交互有兩組角色:
中心設備和外圍設備(Central vs. peripheral);
GATT server vs. GATT client.
Central vs. peripheral:
中心設備和外圍設備的概念針對的是BLE連接本身。Central角色負責scan advertisement。而peripheral角色負責make advertisement。
GATT server vs. GATT client:
這兩種角色取決於BLE連接成功後,兩個設備間通信的方式。
舉例說明:
現 有一個活動追蹤的BLE設備和一個支持BLE的Android設備。Android設備支持Central角色,而BLE設備支持peripheral角 色。創建一個BLE連接需要這兩個角色都存在,都僅支持Central角色或者都僅支持peripheral角色則無法建立連接。
當 連接建立後,它們之間就需要傳輸GATT數據。誰做server,誰做client,則取決於具體數據傳輸的情況。例如,如果活動追蹤的BLE設備需要向 Android設備傳輸sensor數據,則活動追蹤器自然成為了server端;而如果活動追蹤器需要從Android設備獲取更新信息,則 Android設備作為server端可能更合適。
三、許可權及feature:
和經典藍牙一樣,應用使用藍牙,需要聲明BLUETOOTH許可權,如果需要掃描設備或者操作藍牙設置,則還需要BLUETOOTH_ADMIN許可權:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
除了藍牙許可權外,如果需要BLE feature則還需要聲明uses-feature:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
按時required為true時,則應用只能在支持BLE的Android設備上安裝運行;required為false時,Android設備均可正常安裝運行,需要在代碼運行時判斷設備是否支持BLE feature:
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
四、啟動藍牙:
在使用藍牙BLE之前,需要確認Android設備是否支持BLE feature(required為false時),另外要需要確認藍牙是否打開。
如果發現不支持BLE,則不能使用BLE相關的功能。如果支持BLE,但是藍牙沒打開,則需要打開藍牙。
打開藍牙的步驟:
1、獲取BluetoothAdapter
BluetoothAdapter是Android系統中所有藍牙操作都需要的,它對應本地Android設備的藍牙模塊,在整個系統中BluetoothAdapter是單例的。當你獲取到它的示例之後,就能進行相關的藍牙操作了。
獲取BluetoothAdapter代碼示例如下:
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
註:這里通過getSystemService獲取BluetoothManager,再通過BluetoothManager獲取BluetoothAdapter。BluetoothManager在Android4.3以上支持(API level 18)。
2、判斷是否支持藍牙,並打開藍牙
獲取到BluetoothAdapter之後,還需要判斷是否支持藍牙,以及藍牙是否打開。
如果沒打開,需要讓用戶打開藍牙:
private BluetoothAdapter mBluetoothAdapter;
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
五、搜索BLE設備:
通過調用BluetoothAdapter的startLeScan()搜索BLE設備。調用此方法時需要傳入 BluetoothAdapter.LeScanCallback參數。
因此你需要實現 BluetoothAdapter.LeScanCallback介面,BLE設備的搜索結果將通過這個callback返回。
由於搜索需要盡量減少功耗,因此在實際使用時需要注意:
1、當找到對應的設備後,立即停止掃描;
2、不要循環搜索設備,為每次搜索設置適合的時間限制。避免設備不在可用范圍的時候持續不停掃描,消耗電量。
搜索的示例代碼如下:
/**
* Activity for scanning and displaying available BLE devices.
*/
public class DeviceScanActivity extends ListActivity {
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
}
如果你只需要搜索指定UUID的外設,你可以調用 startLeScan(UUID[], BluetoothAdapter.LeScanCallback)方法。
其中UUID數組指定你的應用程序所支持的GATT Services的UUID。
BluetoothAdapter.LeScanCallback的實現示例如下:
private LeDeviceListAdapter mLeDeviceListAdapter;
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
注意:搜索時,你只能搜索傳統藍牙設備或者BLE設備,兩者完全獨立,不可同時被搜索。
六、連接GATT Server:
兩個設備通過BLE通信,首先需要建立GATT連接。這里我們講的是Android設備作為client端,連接GATT Server。
連接GATT Server,你需要調用BluetoothDevice的connectGatt()方法。此函數帶三個參數:Context、autoConnect(boolean)和BluetoothGattCallback對象。調用示例:
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
函數成功,返回BluetoothGatt對象,它是GATT profile的封裝。通過這個對象,我們就能進行GATT Client端的相關操作。BluetoothGattCallback用於傳遞一些連接狀態及結果。
BluetoothGatt常規用到的幾個操作示例:
connect() :連接遠程設備。
discoverServices() : 搜索連接設備所支持的service。
disconnect():斷開與遠程設備的GATT連接。
close():關閉GATT Client端。
readCharacteristic(characteristic) :讀取指定的characteristic。
setCharacteristicNotification(characteristic, enabled) :設置當指定characteristic值變化時,發出通知。
getServices() :獲取遠程設備所支持的services。
等等。
註:
1、某些函數調用之間存在先後關系。例如首先需要connect上才能discoverServices。
2、 一些函數調用是非同步的,需要得到的值不會立即返回,而會在BluetoothGattCallback的回調函數中返回。例如 discoverServices與onServicesDiscovered回調,readCharacteristic與 onCharacteristicRead回調,setCharacteristicNotification與 onCharacteristicChanged回調等。
㈣ android ble為什麼要用service
BLE分為三部分Service、Characteristic、Descriptor,這三部分都由UUID作為唯一標示符。一個藍牙4.0的終端可以包含多個Service,一個Service可以包含多個Characteristic,一個Characteristic包含一個Value和多個Descriptor,一個Descriptor包含一個Value。一般來說,Characteristic是手機與BLE終端交換數據的關鍵,Characteristic有較多的跟許可權相關的欄位,例如PERMISSION和PROPERTY,而其中最常用的是PROPERTY,本文所用的BLE藍牙模塊竟然沒有標準的Characteristic的PERMISSION。Characteristic的PROPERTY可以通過位運算符組合來設置讀寫屬性,例如READ|WRITE、READ|WRITE_NO_RESPONSE|NOTIFY,因此讀取PROPERTY後要分解成所用的組合(本文代碼已含此分解方法)。
㈤ android ble 外設設備怎麼有多組uuid
目前在開發android ble 方面,想要實現程序在後台運行,進入ble設備范圍就給用戶發送通知。我是通過把connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) 的自動連接參數設成true來判斷
㈥ android 藍牙4.0ble的uuid怎麼修改
Generic Attribute Profile (GATT)
通過BLE連接,讀寫屬性類小數據的Profile通用規范。現在所有的BLE應用Profile都是基於GATT的。
Attribute Protocol (ATT)
GATT是基於ATT Protocol的。ATT針對BLE設備做了專門的優化,具體就是在傳輸過程中使用盡量少的數據。每個屬性都有一個唯一的UUID,屬性將以characteristics and services的形式傳輸。
Characteristic
Characteristic可以理解為一個數據類型,它包括一個value和0至多個對次value的描述(Descriptor)。
㈦ ios ble uuid 和android ble mac怎麼一樣
這兩是不一樣的。
㈧ android 藍牙BLE 該怎麼搞,我是想搞個中心和周邊 ,推送消息
Android4.3 規范了BLE的API,但是直到目前的4.4,還有些功能不完善。
在BLE協議中,有兩個角色,周邊(Periphery)和中央(Central);周邊是數據提供者,中央是數據使用/處理者;在iOS SDK裡面,可以把一個iOS設備作為一個周邊,也可以作為一個中央;但是在Android SDK裡面,直到目前最新的Android4.4.2,Android手機只能作為中央來使用和處理數據;那數據從哪兒來?從BLE設備來,現在的很多可穿戴設備都是用BLE來提供數據的。
一個中央可以同時連接多個周邊,但是一個周邊某一時刻只能連接一個中央。
大概了解了概念後,看看Android BLE SDK的四個關鍵類(class):
a)BluetoothGattServer作為周邊來提供數據;BluetoothGattServerCallback返回周邊的狀態。
b)BluetoothGatt作為中央來使用和處理數據;BluetoothGattCallback返回中央的狀態和周邊提供的數據。
因為我們討論的是Android的BLE SDK,下面所有的BluetoothGattServer代表周邊,BluetoothGatt代表中央。
一.創建一個周邊(雖然目前周邊API在Android手機上不工作,但還是看看)
a)先看看周邊用到的class,藍色橢圓
㈨ android藍牙程序開發中,如何確定一台手機當前是伺服器還是客戶端
首先,要操作藍牙,先要在AndroidManifest.xml里加入許可權
<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permissionandroid:name="android.permission.BLUETOOTH" />
然後,看下api,Android所有關於藍牙開發的類都在android.bluetooth包下。 而需要用到了就只有幾個而已:
1.BluetoothAdapter 顧名思義,藍牙適配器,直到我們建立bluetoothSocket連接之前,都要不斷操作它BluetoothAdapter里的方法很多,常用的有以下幾個:cancelDiscovery() 根據字面意思,是取消發現,也就是說正在搜索設備的時候調用這個方法將不再繼續搜索disable()關閉藍牙enable()打開藍牙,這個方法打開藍牙不會彈出提示,更多的時候需要問下用戶是否打開,一下這兩行代碼同樣是打開藍牙,不過會提示用戶:Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);
getAddress()獲取本地藍牙地址getDefaultAdapter()獲取默認BluetoothAdapter,實際上,也只有這一種方法獲取BluetoothAdaptergetName()獲取本地藍牙名稱getRemoteDevice(String address)根據藍牙地址獲取遠程藍牙設備getState()獲取本地藍牙適配器當前狀態(感覺可能調試的時候更需要)isDiscovering()判斷當前是否正在查找設備,是返回true***isEnabled()判斷藍牙是否打開,已打開返回true,否則,返回false***(String name,UUID uuid)根據名稱,UUID創建並返回BluetoothServerSocket,這是創建BluetoothSocket伺服器端的第一步startDiscovery()開始搜索,這是搜索的第一步。 2.BluetoothDevice看名字就知道,這個類描述了一個藍牙設備(UUIDuuid)根據UUID創建並返回一個BluetoothSocket這個方法也是我們獲取BluetoothDevice的目的——創建BluetoothSocket
這個類其他的方法,如getAddress(),getName(),同BluetoothAdapter;
3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不過了,既然是Socket,方法就應該都差不多,這個類一種只有三個方法
兩個重載的accept(),accept(inttimeout)兩者的區別在於後面的方法指定了過時時間,需要注意的是,執行這兩個方法的時候,直到接收到了客戶端的請求(或是過期之後),都會阻塞線程,應該放在新線程里運行。
還有一點需要注意的是,這兩個方法都返回一個BluetoothSocket,最後的連接也是伺服器端與客戶端的兩個BluetoothSocket的連接。
4.BluetoothSocket,跟BluetoothServerSocket相對,是客戶端一共5個方法,不出意外,都會用到close(),關閉connect()連接getInptuStream()獲取輸入流getOutputStream()獲取輸出流getRemoteDevice()獲取遠程設備,這里指的是獲取bluetoothSocket指定連接的那個遠程藍牙設備 。