Android 手机可以通过 BLE 被ble 扫描不到目标设备到吗

Android 手机可以通过 BLE 被扫描到吗_百度知道
Android 手机可以通过 BLE 被扫描到吗
提问者采纳
还可以作为Peripheral就我所知,iOS对BLE的支持要好一些。也许和 @Vincent gao 所提一样,需要查看底层源码来实现。相比之下。不但支持作为Central。这样两部手机可以互相发现和通信,Android官方文档(Bluetooth Low Energy)目前还没有提到如何支持Peripheral
来自团队:
其他类似问题
为您推荐:
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁蓝牙4.0 ble|蓝牙4.0 android ble官方 demo-东坡下载
东坡下载:内容最丰富最安全的下载站!|||
→ 蓝牙4.0 android ble 官方 demo
蓝牙4.0 android ble介绍
目前有蓝牙4.0功能的手机就只有几款,摩托罗拉的刀锋系列、HTC1x和三星GS3,HTC1x和三星GS3虽然有蓝牙4.0的硬件设备但迟迟没有成熟相关SDK可以进行开发,开源的博通BLE SDK开发出来的没有手机可以使用。Android蓝牙系统分为四个层次,内核层、BlueZ库、BlueTooth的适配库、BlueTooth的JNI部分、Java框架层、应用层。下面先来分析Android的蓝牙协议栈。继IOS去年支持蓝牙BLE之后,Android最新的4.3版本也有了官方的支持。受软硬件限制,目前的尝试基于第三方SDK。&开发过程比我想象中的要难,这里所说的难不是编码,不是架构,不是逻辑上的难。而是,找遍了整个百度,google。也没有看到太多关于Android开发BLE设备的资料。查找资料的过程中,看了从学习android以来一时间最多的全英文网站。
蓝牙4.0ble相关软件
电脑版相关
手机版相关
(18)个资源
蓝牙4.0 android ble 官方 demo
江苏(全通用) 网友 客人 发表于:
下载看看,希望有帮助
7.7M / 04-22 / 6.0 绿色汉化版(附注册码)
3.1M / 01-23 / 2.0.0.0 官方安装版
112KB / 10-23 / 1.0 绿色免费版
33.7M / 10-12 / 3.3 官方正式版
12.8M / 09-23 / 3.2.0.290 官方最新免费版
本类软件排行
装机必备软件
压缩阅读卸载恢复Android BLE开发之Android手机与BLE终端通信 - 推酷
Android BLE开发之Android手机与BLE终端通信
&,引用必须注明出处!
最近穿戴设备发展得很火,把相关技术也带旺了,其中一项是BLE(
)。BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备。Android 4.3才开始支持BLE API,所以请各位客官把
运行在蓝牙4.0和Android 4.3及其以上的系统,另外本文所用的BLE终端是一个蓝牙4.0的串口蓝牙模块。
PS:我的i9100刷了4.4系统后,竟然也能跟BLE蓝牙模块通信。
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 4.3 Sample的BluetoothLeGatt,把冗余代码去掉,获取的BLE设备信息都通过LOG.E,还有一些必要的读写蓝牙方法,应该算是简化到大家一看就可以懂了。本文代码可以到
下载。接下来贴出本文运行的结果,
首先是连接BLE设备后,枚举出设备所有
Characteristic
Descriptor,并且手机会往BLE设备写入“send data-&”字符串(见PC串口助手的截图):
04-21 18:28:25.465: E/DeviceScanActivity(12254): --&service type:PRIMARY
04-21 18:28:25.465: E/DeviceScanActivity(12254): --&includedServices size:0
04-21 18:28:25.465: E/DeviceScanActivity(12254): --&service uuid:0-805f9b34fb
04-21 18:28:25.465: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.465: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.465: E/DeviceScanActivity(12254): ----&char property:READ
04-21 18:28:25.465: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char property:READ
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char property:READ|WRITE|
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.470: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.475: E/DeviceScanActivity(12254): ----&char property:READ|WRITE|
04-21 18:28:25.475: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.475: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.475: E/DeviceScanActivity(12254): ----&char property:READ
04-21 18:28:25.475: E/DeviceScanActivity(12254): --&service type:PRIMARY
04-21 18:28:25.475: E/DeviceScanActivity(12254): --&includedServices size:0
04-21 18:28:25.475: E/DeviceScanActivity(12254): --&service uuid:0-805f9b34fb
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char uuid:0-805f9b34fb
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char property:INDICATE
04-21 18:28:25.480: E/DeviceScanActivity(12254): --------&desc uuid:0-805f9b34fb
04-21 18:28:25.480: E/DeviceScanActivity(12254): --------&desc permission:UNKNOW
04-21 18:28:25.480: E/DeviceScanActivity(12254): --&service type:PRIMARY
04-21 18:28:25.480: E/DeviceScanActivity(12254): --&includedServices size:0
04-21 18:28:25.480: E/DeviceScanActivity(12254): --&service uuid:0000ffe0-00-fb
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char uuid:0000ffe1-00-fb
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char permission:UNKNOW
04-21 18:28:25.480: E/DeviceScanActivity(12254): ----&char property:READ|WRITE_NO_RESPONSE|NOTIFY|
04-21 18:28:25.490: E/DeviceScanActivity(12254): --------&desc uuid:0-805f9b34fb
04-21 18:28:25.490: E/DeviceScanActivity(12254): --------&desc permission:UNKNOW
04-21 18:28:25.490: E/DeviceScanActivity(12254): --------&desc uuid:0-805f9b34fb
04-21 18:28:25.490: E/DeviceScanActivity(12254): --------&desc permission:UNKNOW
04-21 18:28:26.025: E/DeviceScanActivity(12254): onCharRead BLE DEVICE read 0000ffe1-00-fb -& 00
这里红字是由BluetoothGattCallback的onCharacteristicRead()回调而打出Log
以下Log是PC上的串口工具通过BLE模块发送过来的
04-21 18:30:18.260: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:18.745: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:19.085: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:19.350: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:19.605: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:19.835: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:20.055: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:20.320: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:20.510: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:20.735: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
04-21 18:30:21.000: E/DeviceScanActivity(12254): onCharWrite BLE DEVICE write 0000ffe1-00-fb -& send data to phone
接下来贴出本文核心代码:
public class DeviceScanActivity extends ListActivity {
private final static String TAG = DeviceScanActivity.class.getSimpleName();
private final static String UUID_KEY_DATA = &0000ffe1-00-fb&;
private LeDeviceListAdapter mLeDeviceListA
/**搜索BLE终端*/
private BluetoothAdapter mBluetoothA
/**读写BLE终端*/
private BluetoothLeClass mBLE;
private boolean mS
private Handler mH
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.title_devices);
mHandler = new Handler();
// 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();
// Initializes a Bluetooth adapter.
For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
//开启蓝牙
mBluetoothAdapter.enable();
mBLE = new BluetoothLeClass(this);
if (!mBLE.initialize()) {
Log.e(TAG, &Unable to initialize Bluetooth&);
//发现BLE终端的Service时回调
mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);
//收到BLE终端数据交互的事件
mBLE.setOnDataAvailableListener(mOnDataAvailable);
protected void onResume() {
super.onResume();
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
mBLE.disconnect();
protected void onStop() {
super.onStop();
mBLE.close();
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null)
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning =
mBLE.connect(device.getAddress());
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
public void run() {
mScanning =
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}, SCAN_PERIOD);
mScanning =
mBluetoothAdapter.startLeScan(mLeScanCallback);
mScanning =
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
* 搜索到BLE终端服务的事件
private BluetoothLeClass.OnServiceDiscoverListener mOnServiceDiscover = new OnServiceDiscoverListener(){
public void onServiceDiscover(BluetoothGatt gatt) {
displayGattServices(mBLE.getSupportedGattServices());
* 收到BLE终端数据交互的事件
private BluetoothLeClass.OnDataAvailableListener mOnDataAvailable = new OnDataAvailableListener(){
* BLE终端数据被读的事件
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS)
Log.e(TAG,&onCharRead &+gatt.getDevice().getName()
+characteristic.getUuid().toString()
+Utils.bytesToHexString(characteristic.getValue()));
* 收到BLE终端写入数据回调
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.e(TAG,&onCharWrite &+gatt.getDevice().getName()
+& write &
+characteristic.getUuid().toString()
+new String(characteristic.getValue()));
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
private void displayGattServices(List&BluetoothGattService& gattServices) {
if (gattServices == null)
for (BluetoothGattService gattService : gattServices) {
//-----Service的字段信息-----//
int type = gattService.getType();
Log.e(TAG,&--&service type:&+Utils.getServiceType(type));
Log.e(TAG,&--&includedServices size:&+gattService.getIncludedServices().size());
Log.e(TAG,&--&service uuid:&+gattService.getUuid());
//-----Characteristics的字段信息-----//
List&BluetoothGattCharacteristic& gattCharacteristics =gattService.getCharacteristics();
for (final BluetoothGattCharacteristic
gattCharacteristic: gattCharacteristics) {
Log.e(TAG,&----&char uuid:&+gattCharacteristic.getUuid());
int permission = gattCharacteristic.getPermissions();
Log.e(TAG,&----&char permission:&+Utils.getCharPermission(permission));
int property = gattCharacteristic.getProperties();
Log.e(TAG,&----&char property:&+Utils.getCharPropertie(property));
byte[] data = gattCharacteristic.getValue();
if (data != null && data.length & 0) {
Log.e(TAG,&----&char value:&+new String(data));
//UUID_KEY_DATA是可以跟蓝牙模块串口通信的Characteristic
if(gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA)){
//测试读取当前Characteristic数据,会触发mOnDataAvailable.onCharacteristicRead()
mHandler.postDelayed(new Runnable() {
public void run() {
mBLE.readCharacteristic(gattCharacteristic);
//接受Characteristic被写的通知,收到蓝牙模块的数据后会触发mOnDataAvailable.onCharacteristicWrite()
mBLE.setCharacteristicNotification(gattCharacteristic, true);
//设置数据内容
gattCharacteristic.setValue(&send data-&&);
//往蓝牙模块写入数据
mBLE.writeCharacteristic(gattCharacteristic);
//-----Descriptors的字段信息-----//
List&BluetoothGattDescriptor& gattDescriptors = gattCharacteristic.getDescriptors();
for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
Log.e(TAG, &--------&desc uuid:& + gattDescriptor.getUuid());
int descPermission = gattDescriptor.getPermissions();
Log.e(TAG,&--------&desc permission:&+ Utils.getDescPermission(descPermission));
byte[] desData = gattDescriptor.getValue();
if (desData != null && desData.length & 0) {
Log.e(TAG, &--------&desc value:&+ new String(desData));
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致大家好!欢迎光临我的 Android 技术博客!
本博客旨在交流与 Android 操作系统相关的各种技术及信息。
博客内的文章会尽量以开源的形式提供给大家,希望我们能相互交流,共同提高!
有不足之处,请不吝赐教!
我的邮箱:zh.weir&#
我的新浪微博:@囧虎张建伟
2013年12月
24252627282930123456781011121314151618192021222324252627282930311234
评论 - 290
留言簿(18)
随笔分类(24)
随笔档案(18)
文章档案(1)
阅读排行榜
评论排行榜Android 手机可以通过 BLE 被扫描到吗_百度知道
Android 手机可以通过 BLE 被扫描到吗
提问者采纳
android5.0以上手机BLE才支持peripheral模式,并且可能需要APP的支持。
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 ble 扫描不到目标设备 的文章

 

随机推荐