RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
android蓝牙4.0,Android蓝牙耳机

安卓手机的蓝牙4.0和苹果的蓝牙4.0一样吗

一样,蓝牙4.0只是模块升级,现在的很多android 4.0一下的手机无法与蓝牙4.0设备通信,android4.2版本会去除linux原先的bluez蓝牙协议栈,用博通公司的bluedroid协议栈,估计这个android的release 可以支持蓝牙4.0.。iphone4s以上的苹果手机,都可以支持蓝牙4.0

10年的红岗网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整红岗建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“红岗网站设计”,“红岗网站推广”以来,每个客户项目都认真落实执行。

android蓝牙4.0怎么设置蓝牙写权限

基本步骤:

获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();

如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。

判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。

启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);

startActivity(in); ,200就表示200秒。

获取蓝牙适配器中已经配对的设备SetBluetoothDevice device=blueadapter.getBondedDevices();

还需要在androidManifest.xml中声明蓝牙的权限

uses-permission android:name="android.permission.BLUETOOTH" /

uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /

接下来就是根据自己的需求对BluetoothAdapter 的操作了。

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(ListBluetoothGattService gattServices) {

if (gattServices == null)

return;

String uuid = null;

ArrayListHashMapString, String gattServiceData = new ArrayList();

ArrayListArrayListHashMapString, String gattCharacteristicData = new ArrayList();

mGattCharacteristics = new ArrayList();

// Loops through available GATT Services.

for (BluetoothGattService gattService : gattServices) {

HashMapString, 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);

ArrayListHashMapString, String gattCharacteristicGroupData = new ArrayList();

ListBluetoothGattCharacteristic gattCharacteristics = gattService.getCharacteristics();

ArrayListBluetoothGattCharacteristic charas = new ArrayList();

// Loops through available Characteristics.

for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

charas.add(gattCharacteristic);

HashMapString, 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("onDescriptorWriteonDescriptorWrite = " + 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中蓝牙2.0和4.0的区别是什么?

最主要的区别就是蓝牙2.0的传输速度没有蓝牙4.0快。以下为蓝牙各版本的说明。\x0d\x0a\x0d\x0a1.1 为最早期版本,传输率约在748~810kb/s,因是早期设计,容易受到同频率之产品所干扰下影响通讯质量。\x0d\x0a蓝牙1.2标准\x0d\x0a1.2 同样是只有 748~810kb/s 的传输率,但在加上了(改善 Software)抗干扰跳频功能。\x0d\x0a蓝牙2.0标准\x0d\x0a2.0 是 1.2 的改良提升版,传输率约在 1.8M/s~2.1M/s,开始支持双工模式——即一面作语音通讯,同时亦可以传输档案/高质素图片,2.0 版本当然也支持 Stereo 运作。\x0d\x0a应用最为广泛的是Bluetooth 2.0+EDR标准,该标准在2004年已经推出,支持Bluetooth 2.0+EDR标准的产品也于2006年大量出现。\x0d\x0a虽然Bluetooth 2.0+EDR标准在技术上作了大量的改进,但从1.X标准延续下来的配置流程复杂和设备功耗较大的问题依然存在。\x0d\x0a蓝牙2.1标准\x0d\x0a2007年8月2日,蓝牙技术联盟今天正式批准了蓝牙2.1版规范,即“蓝牙2.1+EDR”,可供未来的设备自由使用。和2.0版本同时代产品,目前仍然占据蓝牙市场较大份额,相对2.0版本主要是提高了待机时间2倍以上,技术标准没有根本性变化。\x0d\x0a蓝牙3.0标准\x0d\x0a2009年4月21日,蓝牙技术联盟(Bluetooth SIG)正式颁布了新一代标准规范"Bluetooth Core Specification Version 3.0 High Speed"(蓝牙核心规范3.0版 ),蓝牙3.0的核心是"Generic Alternate MAC/PHY"(AMP),这是一种全新的交替射频技术,允许蓝牙协议栈针对任一任务动态地选择正确射频。\x0d\x0a蓝牙3.0的数据传输率提高到了大约24Mbps(即可在需要的时候调用802.11 WI-FI用于实现高速数据传输)。在传输速度上,蓝牙3.0是蓝牙2.0的八倍,可以轻松用于录像机至高清电视、PC至PMP、UMPC至打印机之间的资料传输,但是需要双方都达到此标准才能实现功能。\x0d\x0a蓝牙4.0标准\x0d\x0a蓝牙4.0规范于2010年7月7日正式发布,新版本的最大意义在于低功耗,同时加强不同OEM厂商之间的设备兼容性,并且降低延迟,理论最高传输速度依然为24Mbps(即3MB/s),有效覆盖范围扩大到100米(之前的版本为10米)。该标准芯片被大量的手机、平板所采用,如苹果The New iPad平板电脑,以及苹果iPhone 5、魅族MX4、HTC One X等手机上带有蓝牙4.0功能。\x0d\x0a蓝牙4.1标准\x0d\x0a蓝牙4.1于2013年12月6日发布,与LTE无线电信号之间如果同时传输数据,那么蓝牙4.1可以自动协调两者的传输信息,理论上可以减少 其它信号对蓝牙4.1的干扰。改进是提升了连接速度并且更加智能化,比如减少了设备之间重新连接的时间,意味着用户如果走出了蓝牙4.1的信号范围并且断开连接的时间不算很长,当用户再次回到信号范围中之后设备将自动连接,反应时间要比蓝牙4.0更短。最后一个改进之处是提高传输效率,如果用户连接的设备 非常多,比如连接了多部可穿戴设备,彼此之间的信息都能即时发送到接接收设备上。\x0d\x0a除此之外,蓝牙4.1也为开发人员增加了更多的灵活性,这个改变对普通用户没有很大影响,但是对于软件开发者来说是很重要的,因为为了应对逐渐兴起的可穿戴设备,那么蓝牙必须能够支持同时连接多部设备。\x0d\x0a目前支持该标准的手机还比较少,三星GALAXY Note4则是其中具有代表性的一款。\x0d\x0a蓝牙4.2标准\x0d\x0a2014年12月4日,最新的蓝牙4.2标准颁布,改善了数据传输速度和隐私保护程度,并接入了该设备将可直接通过IPv6和6LoWPAN接入互联网。在新的标准下蓝牙信号想要连接或者追踪用户设备必须经过用户许可,否则蓝牙信号将无法连接和追踪用户设备。\x0d\x0a速度方面变得更加快速,两部蓝牙设备之间的数据传输速度提高了2.5倍,因为蓝牙智能(Bluetooth Smart)数据包的容量提高,其可容纳的数据量相当于此前的10倍左右。蓝牙的版本自然是越高级越好,考虑到传输距离和功耗的问题,最新的蓝牙4.1是优选,但是目前市场上蓝牙4.1的产品并不多,而主流的蓝牙4.0产品性价比更高,至于蓝牙3.0、2.1及以下的版本已经失去选购的价值。

如何使用android原生BLE蓝牙进行操作?

之前的涉及的物联网项目中使用的: BLE 低功耗蓝牙(蓝牙4.0), 支持android 4.3以上的手机

主从关系: BLE低功耗蓝牙只能做从端设备 ,一个蓝牙主端设备,可同时与7个蓝牙从端设备进行通讯

1)低功耗

低功耗的原理:

1\低功耗蓝牙仅使用了3个广播通道,传统蓝牙技术采用 16~32 个频道

2\每次广播开启时间也由传统的 22.5ms 减少到 0.6~1.2ms(毫秒)

2)传输距离极大提高

传统蓝牙传输距离为 2~10m,而蓝牙4.0的有效传输距离可达到 60~100m

3)安全性

使用AES-128 CCM加密算法进行数据包加密和认证。

更多BLE蓝牙的解析参考博客 : BLE4.0教程一 蓝牙协议连接过程与广播分析

添加权限

打开蓝牙

1.先拿到BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

2.再拿到BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

判断是否打开蓝牙

未打开弹出 系统弹框 ,除了 魅族手机 是打开系统设置

设备/手机都是蓝牙信号

在回调方法中:

一般在扫描的过程中,我们还会设置 设备过滤原则 (因为我只想要搜索到我们想要的设备,忽略无关设备)

如:从 scanRecord -- beacon -- beacon.type == 0xFF代表Manufacture,通过与嵌入式软件定义 自己的 Manufacture值即可

用BluetoothDevice得到BluetoothGatt:

断连:

关键问题:连接后一般要做什么事?

( 必须在刚连接成功后2秒内app写一个值给设备,否则会被设备断开连接)

主要是读写 characteristic

gatt.wirteCharacteristic(mCurrentcharacteristic);

gatt.readCharacteristic(characteristic);

bluetoothGatt.setCharacteristicNotification(data, true);

真实工作中使用的蓝牙库BlueToothKit请参考我的另一篇博客:

android蓝牙入门知识和优秀蓝牙第三方库BluetoothKit的使用

android 4.0 蓝牙开发 怎么入手

本文介绍Android ble 蓝牙4.0,也就是说API level = 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level 18,也是用不了蓝牙4.0的哦。

首先发一下官方的demo,有兴趣的可以过去看看:。android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地址:。

一、了解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

扫描后发现可连接的设备,获取已经连接的设备

二、开启蓝牙权限

uses-permission android:name="android.permission.BLUETOOTH"/

uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/

uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/

如果 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比较清晰啊


当前名称:android蓝牙4.0,Android蓝牙耳机
文章转载:http://scyingshan.cn/article/hospeo.html