此例子基于 android demo
首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限
注:Android 6.0后需要加上
这个权限
然后,看下api,Android所有关于蓝牙开发的类都在android.bluetooth包下,如下图,只有8个类
而我们需要用到了就只有几个而已:
1.BluetoothAdapter
顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它BluetoothAdapter里的方法很多,常用的有以下几个:
cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索
disable()关闭蓝牙
enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:
Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);
getAddress()获取本地蓝牙地址
getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter
getName()获取本地蓝牙名称
getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备
getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)
isDiscovering()判断当前是否正在查找设备,是返回true
isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false
listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步
startDiscovery()开始搜索,这是搜索的第一步
2.BluetoothDevice
看名字就知道,这个类描述了一个蓝牙设备
createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返回一个BluetoothSocket
getState() 蓝牙状态这里要说一下,只有在 BluetoothAdapter.STATE_ON 状态下才可以监听,具体可以看andrid api;
3.BluetoothServerSocket
如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,这个类一种只有三个方法两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!
close()这个就不用说了吧,翻译一下——关闭!
4.BluetoothSocket
跟BluetoothServerSocket相对,是客户端一共5个方法,不出意外,都会用到
close(),关闭
connect()连接
getInptuStream()获取输入流
getOutputStream()获取输出流
getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备
1、获取本地蓝牙适配器
2、打开蓝牙
if(!mAdapter.isEnabled()){
//弹出对话框提示用户是后打开
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
//不做提示,强行打开
// mAdapter.enable();
}
是第一步,可以你会发现没有返回的蓝牙设备,怎么知道查找到了呢?向下看,不要急
2)定义BroadcastReceiver,关于BroadcastReceiver不多讲了,不是今天的讨论内容,代码如下
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); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { Log.v(TAG, "find device:" + device.getName() + device.getAddress()); } } //搜索完成 else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { setTitle("搜索完成"); if (mNewDevicesAdapter.getCount() == 0) { Log.v(TAG, "find over"); } } } };
这样,没当查找到新设备或是搜索完成,相应的操作都在上段代码的两个if里执行了,不过前提是你要先注册
BroadcastReceiver,具体代码如下
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter);
1)服务器端:
//http://www.uuidgenerator.com 申请
android.bluetooth下有8个类,还有4个类没有用到,那4个类里定义的都是常量,我也没用到它们..
最后把我找到的几个蓝牙的例子附在后面,希望从事软件开发,尤其是Android开发的朋友以后多沟通、多分享!
补充一下,使设备能够被搜索
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enabler,REQUEST_DISCOVERABLE);
demo是包括客户端和服务端;分别放到两个手机上就可以通信;大家改改应该就可以用;
http://download.csdn.net/detail/q/
这个是官方的demo:
http://download.csdn.net/detail/q/
https://edu.csdn.net/course/detail/24723Android bluetooth 蓝牙通信 视频教程
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/199140.html原文链接:https://javaforall.net
