android 怎么连接android 蓝牙设备类型

Android中经典蓝牙开发步骤 (流程)
蓝牙在我们做智能手表中,必须使用到的。即使不同的需求开发,但也可以抽取出下面的步骤。
下面的流程,如果已经完成了这一步,就可以去到下一步。比如说,已经打开了蓝牙,那么蓝牙肯定是可用的。这才真的沒必要检测蓝牙是否可用了。
如果已经打开了,当然也沒有必要再次执行打开的代码啦!是吧!又比如说,已经配对了蓝牙,就沒必要再配对了。在连接之前进行检查一下就可以了嘛!
1、检查是否支持蓝牙设备:
我们通过BluetoothAdapter这个类去获取适配器,如果沒有则不支持蓝牙通信,也就是该设备沒有蓝牙模块。
不管是我们自己的设备一定有蓝牙还是別人的设备知道一定有。大家都要养成习惯,在使用之前检查一下是否支持蓝牙。
* 返回值 true 表示可以用嘛,否则不可以
* 描述:这个方法用于检查蓝牙是否可用
public boolean checkBtIsValueble() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
根据返回值,如果不支持是吧,那只好提示用户不支持蓝牙。如果支持蓝牙,则进行下一步:
2、判断蓝牙可不可用(有沒有打开?3:打开蓝牙)
判断蓝牙有沒有打开的话,可以通过适配器的isEnable()方法来判断。
打开蓝牙的方法有两种,一种是静默打开(个別不同,可能也要用户授权),另外一种则需要让用户授权,可以检测到打开的状态。
第一种打开方式,通过意图的形式来打开,这种形式不需要声明权限,但是要经过用户的授权,请看码:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
接着呢?看到 ForResult当然是去接收结果啦,是吧!
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//判断是不是启动蓝牙的结果
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, &蓝牙开启成功...&, Toast.LENGTH_SHORT).show();
isOpenBlueToolth =
Toast.makeText(this, &蓝牙开启失败...&, Toast.LENGTH_SHORT).show();
isOpenBlueToolth =
RESULT_OK则表示成功,RESULT_CANCELED则表示取消了。
或者你也可以监听广播(但要記得注销广播),创建一个广播接收者,设置过滤为:ACTION_STATE_CHANGED
本质:蓝牙的状态发生改变这后,系统就会发出广播的,具体的含义如下:
EXTRA_STATE:直接翻译是额外状态
EXTRA_PREVIOUS_STATE: 这个是这前的状态
这上面两个状态的值是下面的其中一个:
STATE_TURNING_ON:正在玩命打开中...
STATE_ON:已经开启了
STATE_TURNING_OFF:使劲关闭中...
STATE_OFF:已经关闭了
第二种很简单,小手一抖,一行代码的事。但是要添加权限。为静默打开形式,对于某些系统来说,是无效的。还是需要用户的授权。
if (!defaultAdapter.isEnabled()) {
defaultAdapter.enable();
好,就这样子,就打开了,核心代码就一行。enable()就可以了。不要忘记权限!
PS:关闭蓝牙为disable();
3、根据不同的角色,明确需求
这里面的角色,只有两种嘛,要么是客户端,要么是服务端。其实蓝牙是BluetoothSocket,我们小时候学java的时候就学过网络,也有Scoket,是吧!
首先是先有服务端,阻塞式地等客户端连接进来。
这里也一样,要分清楚是服务端还是客户端,这是第一点。另外就是要記得,关于网络访问,数据传输这些是耗时操作。要注意线程哈!
如果是客户端,那我们的目标是什么?当然是连接到服务端去,是吧!首先呢,我们要找到服务端。这时要进行蓝牙扫描啦!
怎么扫描周围的服务端呢?看码:
//扫描蓝牙
isDiscovering = defaultAdapter.startDiscovery();
//注册一个广播接收者來获取到扫描的蓝牙设备
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
广播接收者的代码如下:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, &扫描到了 --- & name : & + device.getName() + &-----& address:& + device.getAddress() + isDiscovering);
mAdapter.add(device.getAddress());
mAdapter.notifyDataSetChanged();
解释一下吧:我们通过开启扫描,然后就进行对周围的设备进行扫描啦,記得好像是扫描12秒的。不太清楚了,想查个究竟的可以上网问,或者自己用秒表测試一下哈,嘻嘻!
每扫描到一个设备的话,就会收到一次广播啦。这里要一定要注意哈,设备的名字可能是空的,但地址一定是有的。所以在使用设备名字时,一定要記得判空哪!
就这样子,我们静悄悄地就可以发现周围的设备了,或许你又有疑问了,为什么手机谁连谁都可以呢?这到底是为什么呢?因为他们同时实现了客户端和服务端的功能。实际开发中也可以根据需求这样做。
有了设备之后,我们可以获取到设别的物理地址。接下来就是要进行配对了。这里要说一下的时, 配对和连接是两个不同的概念哦!
你可以理解为,配对是为了验证身份,而连接则是创建传送数据的通道。
在配对之前,要获取到远程的设备(服务端),怎么获取呢,看下面的代码吧:
if (mBluetoothAdapter == null) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
updateMsg(&去获取适配器...&);
updateMsg(&去获取远程设备...&);
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress);
updateMsg(&去获取...BluetoothSocket...&);
socket = mBluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(UUID_STR));
} catch (IOException e) {
updateMsg(&错误:socket获取失败...& + e.toString());
这里面要理解的是通过物理地址来获取到远程的设备,然后需要UUID作为了一个标识。类比我们小时候学的Socket,这里的mac地址呢,就类似于Ip地址,而UUID则类似于端口号。这样相信聪明的你一定理解了是吧!
怎么进行配对和判断有沒有配对呢?
先来判断是否有配对吧:
在配对之前呢,我们要习惯先取消掉发现设备(Discovery()):cancelDiscovery();
updateMsg(&取消蓝牙查找(搜索)...&);
mBluetoothAdapter.cancelDiscovery();
// 连接建立之前的先配对
if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
Method creMethod = BluetoothDevice.class.getMethod(&createBond&);
updateMsg(&正开始进行配对蓝牙...&);
creMethod.invoke(mBluetoothDevice);
updateMsg(&已经配对&);
} catch (Exception e) {
updateMsg(&无法进行配对...& + e.toString());
如果上面沒有什么问题的主知,那么就可以连接了,是吧!哈哈,此处应有掌声哈!
话不多说,程序员更多的是用代码来表达自己的想法:
//连接操作
socket.connect();
updateMsg(&连接状态...& + socket.isConnected());
} catch (IOException e) {
updateMsg(&连接失败& + e.toString());
if (socket != null) {
socket.close();
} catch (IOException e2) {
updateMsg(&关闭socket失败& + e2.toString());
这样子,连接了。这和Socket是一样的道理嘛!
到这里的话,客户端的准备工作就搞定了。但是这些操作要在子线程中完成,知道嗎?
接下来,当然是发数据和接收数据,可以通过getInputStream(),来获取输入流,用getOutputStream来获取输入流。
使用完成之后要記得关闭流,对于异常可以统一处理,使用代号也行,按自己的习惯或者公司的开发标准。
到这里就不说发送数据了,你爱咋咋地!下面就说说服务端吧!
3.2服务端的业务逻辑
蓝牙和Socket不同的是mac地址不是和ip地址那样预先知道的。所以服务端要被客户端扫描到。一般情况下,是不可见的。那么这个时候,客户端对服务端进行扫描时,就需要让服务端可见啦!
Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//设置蓝牙的可见时间
btIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
startActivity(btIntent);
Toast.makeText(WearMainActivity.this, &正打开蓝牙可见..&, Toast.LENGTH_SHORT).show();
这里参数哈,我靠,一看3600是吧,那我就不客气了,来个10000怎么样呢?其实,最多只能是3600秒。默认貌似是120秒吧,我忘记了,老了!
作为服务端,它应该和ServiceSocket一样,去等待着客户端连接进来。一般在子线程中开一个死循环去等待。由于app在多数情况下是一对一的。accept方法是阻塞的。所以呢,可以不死循环去accept()客户端进来。
当然,这里要根据需求来定啦。不能这么死板。多数情况下,我们会使用可控制的循环去等待客户端进来。下面例子直接等待一个连接进来就完事了哈:
socket = mmServerSocket.accept();
} catch (IOException e) {
updateViewSafely(&accept失败..& + e.toString());
如果有客户端进来的话,那么就可以获取到了socket。那么还是一样的方法,通过socket来获取到输入输出流。
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
接下来想干嘛干嘛去,要注意对异常的处理,关流等操作。一般来说,操作这些数据的在子线程,如果要更新UI,不要搞错线程。通常用Handler来处理消息。
到这里貌似写完了哈,不详细的地方自己想去,也可以问我,反正我也不会告诉你的。有错的地方嘛,可以到社区当众指出!android bluetooth——蓝牙的开启、搜索、配对与连接
android 的blt仅仅支持api 18 android4.3以上,有的功能甚至需要api 19 android4.4;
所以我们在做blt项目之前一定要清楚可用的版本范围。
我要讲述的是打开blt大门的操作。这些操作就是如何打开blt、如何搜索到其他设备、如何配对选中设备、如何通过mac地址连接之前已经配对过的设备以及连接成功的两个(或一对多个)设备如何通讯。
在学习blt知识前要先搞清楚,blt是如何让两个设备之间通讯的。我们先来看一下基本步骤:
1,打开blt。
——1)权限
——2)监听设备是否打开blt
——3)操作,打开或者关闭blt
2,搜索附近设备&让附近设备搜索到
——1)让自身设备可以被附近搜索到,时间最长300s;(随着api的逐渐升级可能不止300s,做了一定的优化处理)
——2)搜索附近可连接的设备;过时的api在接口中回调搜索结果,新的api则要求用广播接收搜索结果。(我们这里使用广播接收结果)
3,与目标设备配对
——1)对于目标设备进行配对,android系统本身会完成配对动作,并且将已经成功配对的设备的mac地址保存起来,以供下次进行自动配对使用。
——2)对进行配对过的设备自动配对。这个动作也是系统帮我们完成的,我们只需要根据系统给我们的状态来判断这个是否已经配对就行了。
4,与成功配对的设备进行连接
——1)如果要对成功配对的设备进行连接,则必须先建立服务器端。服务器端建立只有会线程阻塞等待客户端的连接。
——2)确保建立了服务器端之后再建立客户端。客户端的连接过程也是线程阻塞的。知道连接成功后,服务器端收到消息,则表示配对设备已连接成功。
5,注意事项:
——1)配对成功过的设备无需再次配对,只需要从系统中拿到mac地址进行连接。这个根据系统返回的状态值去判断。
——2)搜索附近设备的操作是一个消耗内存的动作,我们务必在连接设备‘之前‘停止搜索。
——3)设备的配对成功不代表连接成功。配对和连接是两回事且配对在连接动作之前。
——4)当我们的程序明确指出“不需要blt操作了”的时候,及时反注册blt广播,停止blt连接,注销蓝牙对象。而这个操作例如:程序退出、用户无操作超时、逻辑不需要blt的连接了。
以上就是一个蓝牙连接成功之后可以正常传输信息的步骤。
接下来我们再来看看相应的步骤在代码逻辑中的代表。
android:name="android.permission.BLUETOOTH" /&
android:name="android.permission.BLUETOOTH_ADMIN" /&
2,获得蓝牙管理对象。BluetoothManager的主要作用是从中得到我们需要的对象
//@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
//首先获取BluetoothManager
BluetoothManager bluetoothManager=(BluetoothManager) context.getService(Context.BLUETOOTH_SERVICE)
3,获得蓝牙适配器。蓝牙适配器是我们操作蓝牙的主要对象,可以从中获得配对过的蓝牙集合,可以获得蓝牙传输对象等等
if (bluetoothManager != null)
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
3,注册广播来接收蓝牙配对信息。在蓝牙开启前调用
// 用BroadcastReceiver来取得搜索结果
IntentFilter intent = new IntentFilter()
intent.addAction(BluetoothDevice.ACTION_FOUND)
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)
intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
context.registerReceiver(searchDevices, intent)
* 蓝牙接收广播
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Object[] lstName = b.keySet().toArray();
for (int i = 0; i & lstName. i++) {
String keyName = lstName[i].toString();
Log.e("bluetooth", keyName + "&&&" + String.valueOf(b.get(keyName)));
BluetoothD
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
onRegisterBltReceiver.onBluetoothDevice(device);
else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
Log.d("BlueToothTestActivity", "正在配对......");
onRegisterBltReceiver.onBltIng(device);
case BluetoothDevice.BOND_BONDED:
Log.d("BlueToothTestActivity", "完成配对");
onRegisterBltReceiver.onBltEnd(device);
case BluetoothDevice.BOND_NONE:
Log.d("BlueToothTestActivity", "取消配对");
onRegisterBltReceiver.onBltNone(device);
4,反注册广播和清楚蓝牙连接。在不需要使用蓝牙的时候调用
* 反注册广播取消蓝牙的配对
public void unregisterReceiver(Context context) {
context.unregisterReceiver(searchDevices);
if (mBluetoothAdapter != null)
mBluetoothAdapter.cancelDiscovery();
5,判断当前设备是否支持蓝牙,如果支持则打开蓝牙
* 判断是否支持蓝牙,并打开蓝牙
* 获取到BluetoothAdapter之后,还需要判断是否支持蓝牙,以及蓝牙是否打开。
* 如果没打开,需要让用户打开蓝牙:
public void checkBleDevice(Context context) {
if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(enableBtIntent);
Log.i("blueTooth", "该手机不支持蓝牙");
6,搜索蓝牙。搜索后添加到自定义的集合中以供自身程序的逻辑使用。注意:搜索到的结果会在广播中回调过来,这里不做回调。若使用过时的接口,则在当前逻辑里回调结果。我们这里使用广播,不建议使用过时的方法。
* 搜索蓝牙设备
* 通过调用BluetoothAdapter的startLeScan()搜索BLE设备。
* 调用此方法时需要传入 BluetoothAdapter.LeScanCallback参数。
* 因此你需要实现 BluetoothAdapter.LeScanCallback接口,BLE设备的搜索结果将通过这个callback返回。
* 由于搜索需要尽量减少功耗,因此在实际使用时需要注意:
* 1、当找到对应的设备后,立即停止扫描;
* 2、不要循环搜索设备,为每次搜索设置适合的时间限制。避免设备不在可用范围的时候持续不停扫描,消耗电量。
* 如果你只需要搜索指定UUID的外设,你可以调用 startLeScan(UUID[], BluetoothAdapter.LeScanCallback)方法。
* 其中UUID数组指定你的应用程序所支持的GATT Services的UUID。
* 注意:搜索时,你只能搜索传统蓝牙设备或者BLE设备,两者完全独立,不可同时被搜索。
private boolean startSearthBltDevice(Context context) {
checkBleDevice(context);
if (getmBluetoothAdapter().isDiscovering())
stopSearthBltDevice();
Log.i("bluetooth", "本机蓝牙地址:" + getmBluetoothAdapter().getAddress());
mBluetoothAdapter.startDiscovery();
return true;
7,停止搜索蓝牙设备
public boolean stopSearthBltDevice() {
if(mBluetoothAdapter!=null)
return mBluetoothAdapter.cancelDiscovery();
8,连接蓝牙。注意:连接蓝牙的前提是我们已经建立好了蓝牙服务器端。所以,这里我们先建立蓝牙服务器
——1)先建立蓝牙服务器端
* 这个操作应该放在子线程中,因为存在线程阻塞的问题
public void run(Handler handler) {
BluetoothServerSocket
bluetoothServerSocket = tmBluetoothAdapter.listenUsingRfcommWithServiceRecord("com.bluetooth.demo", BltContant.SPP_UUID);
while (true) {
socket = getBluetoothServerSocket().accept();
if (socket != null) {
BltAppliaction.bluetoothSocket =
Message message = new Message();
message.what = 3;
message.obj = socket.getRemoteDevice();
handler.sendMessage(message);
getBluetoothServerSocket().close();
} catch (IOException e) {
getBluetoothServerSocket().close();
} catch (IOException e1) {
e1.printStackTrace();
——2)在蓝牙服务器建立之后,再进行连接蓝牙的操作。
* 尝试连接一个设备,子线程中完成,因为会线程阻塞
* btDev 蓝牙设备对象
* handler 结果回调事件
private void connect(BluetoothDevice btDev, Handler handler) {
mBluetoothSocket = btDev.createRfcommSocketToServiceRecord(BltContant.SPP_UUID);
if (mBluetoothSocket != null)
BltAppliaction.bluetoothSocket = mBluetoothS
Log.d("blueTooth", "开始连接...");
if (getmBluetoothAdapter().isDiscovering())
getmBluetoothAdapter().cancelDiscovery();
if (!getmBluetoothSocket().isConnected()) {
getmBluetoothSocket().connect();
Log.d("blueTooth", "已经链接");
if (handler == null) return;
Message message = new Message();
message.what = 4;
message.obj = btD
handler.sendMessage(message);
} catch (Exception e) {
Log.e("blueTooth", "...链接失败");
getmBluetoothSocket().close();
} catch (IOException e1) {
e1.printStackTrace();
e.printStackTrace();
9,自动连接以往配对成功的设备。注意:连接的前提是服务器端已开启,若没开启则进行8.1的操作
* 尝试配对和连接
public void createBond(BluetoothDevice btDev, Handler handler) {
if (btDev.getBondState() == BluetoothDevice.BOND_NONE) {
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.KITKAT) {
btDev.createBond();
} else if (btDev.getBondState() == BluetoothDevice.BOND_BONDED) {
connect(btDev, handler);
* 获得系统保存的配对成功过的设备,并尝试连接
public void getBltList() {
if (getmBluetoothAdapter() == null) return;
Set&BluetoothDevice& devices = getmBluetoothAdapter().getBondedDevices();
if (devices.size() & 0) {
for (Iterator&BluetoothDevice& it = devices.iterator(); it.hasNext(); ) {
BluetoothDevice device = it.next();
createBond(device, null);
注意:外界只需要调用getBltList();方法即可进行自动连接。
10,输入mac地址自动连接设备。前提是系统原来连接过该地址。
* 输入mac地址进行自动配对
* 前提是系统保存了该地址的对象
public void autoConnect(String address, Handler handler) {
if (getmBluetoothAdapter().isDiscovering()) getmBluetoothAdapter().cancelDiscovery();
BluetoothDevice btDev = getmBluetoothAdapter().getRemoteDevice(address);
connect(btDev, handler);
11,蓝牙连接状态。用于我们判断该蓝牙设备是出于:未配对还是配对未连接还是已连接状态
public String bltStatus(int status) {
String a = "未知状态";
switch (status) {
case BluetoothDevice.BOND_BONDING:
a = "连接中";
case BluetoothDevice.BOND_BONDED:
a = "连接完成";
case BluetoothDevice.BOND_NONE:
a = "未连接/取消连接";
11,蓝牙点击事件。包括蓝牙的打开,关闭,被搜索,断开连接
* 蓝牙操作事件
public void clickBlt(Context context, int status) {
switch (status) {
case BltContant.BLUE_TOOTH_SEARTH:
startSearthBltDevice(context);
case BltContant.BLUE_TOOTH_OPEN:
if (getmBluetoothAdapter() != null)
getmBluetoothAdapter().enable();
case BltContant.BLUE_TOOTH_CLOSE:
if (getmBluetoothAdapter() != null)
getmBluetoothAdapter().disable();
case BltContant.BLUE_TOOTH_MY_SEARTH:
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
context.startActivity(discoverableIntent);
case BltContant.BLUE_TOOTH_CLEAR:
if (getmBluetoothSocket() != null)
getmBluetoothSocket().close();
} catch (IOException e) {
e.printStackTrace();
到此,蓝牙从打开到连接的方法都写完了。最后我们来总结一下。
当我们获得到了bluetoothsocket对象的时候,我们就可以像使用socket编程一样,让两个蓝牙之间传输数据。甚至可以在程序内部监听蓝牙耳机的暂停/播放/音量键等的点击事件。
具体的蓝牙操作,我将放在demo里供大家学习。
没有更多推荐了,&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
Android 获取蓝牙设备类型
摘要:之前我们分析了如何获取已连接的蓝牙设备地址http://blog.csdn.net/jasonwang18/article/details/本篇我们分析如何获取对应蓝牙设备的类型,这个类型和profile不是同一个东西,而是具体蓝牙的设备类型,比如手机、电脑、手柄、蓝牙耳机等我们看到手机搜索到的蓝牙设备类型有三种,手机、电脑和普通蓝牙/***GettheBluetoothdevicetypeoftheremotedevice.**Requires{@linka
之前我们分析了如何获取已连接的蓝牙设备地址
http://blog.csdn.net/jasonwang18/article/details/
本篇我们分析如何获取对应蓝牙设备的类型,这个类型和profile不是同一个东西,而是具体蓝牙的设备类型,比如手机、电脑、手柄、蓝牙耳机等
我们看到手机搜索到的蓝牙设备类型有三种,手机、电脑和普通蓝牙
/** * Get the Bluetooth device type of the remote device. * * Requires {@link android.Manifest.permission#BLUETOOTH} * * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE} * {@link #DEVICE_TYPE_DUAL}. * {@link #DEVICE_TYPE_UNKNOWN} if it's not available */ @RequiresPermission(Manifest.permission.BLUETOOTH) public int getType() { if (sService == null) { Log.e(TAG, &BT not enabled. Cannot get Remote Device type&); return DEVICE_TYPE_UNKNOWN; } try { return sService.getRemoteType(this); } catch (RemoteException e) {Log.e(TAG, &&, e);} return DEVICE_TYPE_UNKNOWN; }
我们看到BluetoothDevice中有一个方法getType,这个返回的是profile类型,也就是
DEVICE_TYPE_CLASSIC 经典蓝牙
DEVICE_TYPE_LE & & & & & &低功耗蓝牙
DEVICE_TYPE_DUAL & & & 双向蓝牙
然而并不是我们想要的东西
/** * Get the Bluetooth class of the remote device. * Requires {@link android.Manifest.permission#BLUETOOTH}. * * @return Bluetooth class object, or null on error */ @RequiresPermission(Manifest.permission.BLUETOOTH) public BluetoothClass getBluetoothClass() { if (sService == null) { Log.e(TAG, &BT not enabled. Cannot get Bluetooth Class&); } try { int classInt = sService.getRemoteClass(this); if (classInt == BluetoothClass.ERROR) return new BluetoothClass(classInt); } catch (RemoteException e) {Log.e(TAG, &&, e);} }
getBluetoothClass返回remoteDevice的class
public static class Major { private static final int BITMASK = 0x1F00; public static final int MISC = 0x0000; public static final int COMPUTER = 0x0100; public static final int PHONE = 0x0200; public static final int NETWORKING = 0x0300; public static final int AUDIO_VIDEO = 0x0400; public static final int PERIPHERAL = 0x0500; public static final int IMAGING = 0x0600; public static final int WEARABLE = 0x0700; public static final int TOY = 0x0800; public static final int HEALTH = 0x0900; public static final int UNCATEGORIZED = 0x1F00; }
BluetoothClass定义了所有主要类型,比如麦克风MISC、COMPUTER、AUDIO等,另外还要主要类型下面的分类
// Devices in the COMPUTER major class public static final int COMPUTER_UNCATEGORIZED = 0x0100; public static final int COMPUTER_DESKTOP = 0x0104; public static final int COMPUTER_SERVER = 0x0108; public static final int COMPUTER_LAPTOP = 0x010C; public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110; public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114; public static final int COMPUTER_WEARABLE = 0x0118;比如所有COMPUTER类型的细分类型
// Devices in PERIPHERAL major class /** * @hide */ public static final int PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500; /** * @hide */ public static final int PERIPHERAL_KEYBOARD = 0x0540; /** * @hide */ public static final int PERIPHERAL_POINTING = 0x0580; /** * @hide */ public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;所有外围设备的细分
/** * Return the major device class component of this {@link BluetoothClass}. * Values returned from this function can be compared with the * public constants in {@link BluetoothClass.Device.Major} to determine * which major class is encoded in this Bluetooth class. * * @return major device class component */ public int getMajorDeviceClass() { return (mClass &; Device.Major.BITMASK); }getMajorDeviceClass获取major的值
04-17 15:36:41.804 /? I/Unity: GetBluetoothClass (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)04-17 15:36:41.811 /? I/Unity-connection: address:20:73:02:61:00:17 BluetoothClass: 15:36:41.811 /? I/Unity-connection: address:20:73:02:61:00:17 BluetoothClass:1280
我们看到major的类型值是1280,也就是PERIPHERAL,外围设备
连接的是一把游戏枪。子类型是PERIPHERAL_KEYBOARD
04-17 16:06:22.305 /? I/Unity: GetBluetoothClass (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)04-17 16:06:22.311 /? I/Unity-connection: address:07:14:11:04:22:35 BluetoothClass: 16:06:22.311 /? I/Unity-connection: address:07:14:11:04:22:35 BluetoothClass:1280
public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;
连接的是游戏手柄,以上两种类型的设备都属于PROFILE_HID类型的设备。
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
Android 获取蓝牙设备类型相关信息,包括
的信息,所有Android 获取蓝牙设备类型相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International}

我要回帖

更多关于 android 蓝牙设备区分 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信