ble40android
❶ android4.2.2 支持 藍牙4.0
藍牙v4.0在2010年就推出了,但除iPhone4S,Galaxy S3, Note2支持藍牙4.0外,Android 4.2原生系統缺乏對4.0的支持,因此4.0的BLE連接尚未大范圍普及。預計低功耗藍牙4.0會隨著Android 4.3的升級得到更普遍的運用。
可參考
http://..com/link?url=gQpKOGVM4BLcFJ2L7cKT-YBEprABEw8j4aCBeIbSjB8i5APTYv_
❷ 誰能給一份開發基於藍牙4.0的android的demo源碼
請參考如下WIKI頁面,基本上目前有提供的BLE的參考代碼都集中到這個頁面了:http://processors.wiki.ti.com/index.php/Category:BluetoothLE
❸ android藍牙4.0接收數據長度
事實上,我們在實際連接操作中(android + ble),不同的ble設備和不同的android設備連接過程都有不同的表現。但一般來說,連接6~7個是沒有問題的
❹ 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)。
❺ Android 手機可以通過 BLE 被掃描到嗎
本文介紹Android ble 藍牙4.0,也就是說API level >= 18,且支持藍牙4.0的手機才可以使用,如果手機系統版本API
level < 18,也是用不了藍牙4.0的哦。
首先發一下官方的demo,有興趣的可以過去看看:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html。android系統4.3以上,手機支持藍牙4.0,具有搜索,配對,連接,發現服務及特徵值,斷開連接等功能,下載地址:http://download.csdn.net/detail/lqw770737185/8116019。
一、了解api及概念
1.1 BluetoothGatt
繼承BluetoothProfile,通過BluetoothGatt可以連接設備(connect),發現服務(discoverServices),並把相應地屬性返回到BluetoothGattCallback
1.2 BluetoothGattCharacteristic
相當於一個數據類型,它包括一個value和0~n個value的描述(BluetoothGattDescriptor)
1.3 BluetoothGattDescriptor
描述符,對Characteristic的描述,包括范圍、計量單位等
1.4 BluetoothGattService
服務,Characteristic的集合。
1.5 BluetoothProfile
一個通用的規范,按照這個規范來收發數據。
1.6 BluetoothManager
通過BluetoothManager來獲取BluetoothAdapter
BluetoothManager bluetoothManager = (BluetoothManager)
getSystemService(Context.BLUETOOTH_SERVICE);
1.7 BluetoothAdapter
一個Android系統只有一個BluetoothAdapter ,通過BluetoothManager 獲取
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
1.8 BluetoothGattCallback
已經連接上設備,對設備的某些操作後返回的結果。這里必須提醒下,已經連接上設備後的才可以返回,沒有返回的認真看看有沒有連接上設備。
private BluetoothGattCallback GattCallback = new BluetoothGattCallback()
{
// 這里有9個要實現的方法,看情況要實現那些,用到那些就實現那些
public void onConnectionStateChange(BluetoothGatt gatt, int status, int
newState){};
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status){};
};
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
BluetoothGatt gatt = device.connectGatt(this, false, mGattCallback);
1.8.1:notification對應onCharacteristicChanged;
gatt.setCharacteristicNotification(characteristic, true);
1.8.2:readCharacteristic對應onCharacteristicRead;
gatt.readCharacteristic(characteristic);
1.8.3: writeCharacteristic對應onCharacteristicWrite;
gatt.wirteCharacteristic(mCurrentcharacteristic);
1.8.4:連接藍牙或者斷開藍牙 對應 onConnectionStateChange;
1.8.5: readDescriptor對應onDescriptorRead;
1.8.6:writeDescriptor對應onDescriptorWrite;
gatt.writeDescriptor(descriptor);
1.8.7:readRemoteRssi對應onReadRemoteRssi;
gatt.readRemoteRssi()
1.8.8:executeReliableWrite對應onReliableWriteCompleted;
1.8.9:discoverServices對應onServicesDiscovered。
gatt.discoverServices()
1.9 BluetoothDevice
掃描後發現可連接的設備,獲取已經連接的設備
二、開啟藍牙許可權如果
android.hardware.bluetooth_le設置為false,可以安裝在不支持的設備上使用,判斷是否支持藍牙4.0用以下代碼就可以了
if
(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
Toast.makeText(this, "設備不支持藍牙4.0", Toast.LENGTH_SHORT).show();
finish();
}
三、對藍牙的啟動關閉操作
1、利用系統默認開啟藍牙對話框
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
2、後台打開藍牙,不做任何提示,這個也可以用來自定義打開藍牙對話框啦
mBluetoothAdapter.enable();
3、後台關閉藍牙
mBluetoothAdapter.disable();
四、掃描設備,連接設備,獲取設備信息 ,斷開連接設備,自行查看官方demo,還是看demo比較清晰啊
❻ 藍牙40,藍牙BLE模塊,藍牙40和藍牙BLE的區別
一、關於藍牙40 藍牙40標准包含兩個藍牙標准,准確的說,是一個雙模的標准,它包含傳統藍牙部分(也有稱之為經典藍牙Classic Bluetooth)和低功耗藍牙部分(Bluetooth Low Energy)。這兩個部分適用於不同的應用或者應用條件。傳統藍牙是在之藍牙40,藍牙BLE模塊,藍牙40和藍牙BLE的區別
❼ 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);
}
}