蓝牙基础知识学习笔记一
蓝牙API都在android.bluetooth
包中,这里详细描述蓝牙连接的类和接口 :
-
表示本地蓝牙适配器(蓝牙无线电)。
BluetoothAdapter
是所有的蓝牙互动的入口点 。用这种方法,你可以发现其他蓝牙设备,查询(配对)设备清单,实例化一个BluetoothDevice
使用一个已知的MAC地址,并创建一个
BluetoothServerSocket听取来自其它设备的通信 。 -
表示远程蓝牙设备。使用这个请求通过与远程设备的
连接
BluetoothSocket或有关设备等作为其名称,地址,类别,和粘接状态查询信息 。 -
表示蓝牙接口插座(类似一个TCP
套接字
)。这是的连接点,它允许应用程序交换数据与其他蓝牙设备通过InputStream和OutputStream。 -
代表一个打开的服务器套接字传入的请求(类似一个TCP监听
ServerSocket的
)。为了连接两个Android设备,一个设备必须打开这个类的一个服务器套接字。当远程蓝牙设备请求连接到这个设备,BluetoothServerSocket
将返回的连接
BluetoothSocket时,连接被接受 。 - 描述蓝牙设备的一般特点和功能。这是一个只读的属性,定义设备的主要和次要设备类和它的服务设置。但是,这并不能可靠地描述所有的蓝牙配置文件和设备支持的服务,但作为一个设备类型的提示非常有用。
- 蓝牙配置文件的接口。Bluetooth配置文件 是基于蓝牙的设备之间的通信的无线接口规范 。一个例子是免提模式。对于更多的配置文件的讨论 ,请参阅资料工作
- 提供手机使用蓝牙耳机的支持。这既包括蓝牙耳机和免提(V1.5)型材。
- 定义有多高品质的音频,可以从一个设备传输到另一个通过蓝牙连接。“A2DP”代表高级音频传输模式。
-
通知
BluetoothProfile
IPC的客户端已连接或断开服务(即运行一个特定的配置文件,内部服务)接口 。 -
- 我们怎么根据提供的接口和类来实现自己的蓝牙应用程序呢?google android 给你个大概的指南:
-
The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following: (使用蓝牙API的步骤)
- Scan for other Bluetooth devices
- Query the local Bluetooth adapter for paired Bluetooth devices
- Establish RFCOMM channels
- Connect to other devices through service discovery
- Transfer data to and from other devices
- Manage multiple connections
- 一、首先在自己的Android应用程序的AndroidManifest.xml中添加使用蓝牙的权限;
-
<manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> </manifest>
- 二、创建Bluetoothadapter 本地蓝牙设备
-
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBlBluetoothAdapter == null){
Log.i(TAG, "本机器不支持蓝牙...");
}else{
//判断本机蓝牙是否已经开启
if(!mBlBluetoothAdapter.isEnabled()){
//没有开启蓝牙,则提示用户是否开启蓝牙功能
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
//3 查询配对设备
Set<BluetoothDevice> devices = mBlBluetoothAdapter.getBondedDevices();
if(devices.size() > 0){
//遍历已连接的远程蓝牙设备
for(Iterator<BluetoothDevice> iterator = devices.iterator(); iterator.hasNext();){
//得到远程连接设备
BluetoothDevice device = iterator.next();
Log.i(TAG, "远程设备的mac地址:"+device.getAddress()+" 名称"+device.getName()+" 状态"+device.getBondState());
}
}
//4 注册// Register the BroadcastReceiver 用于发现远程蓝牙设备
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
//5 设置可被发现Enabling discoverability
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//有效持续时间300秒,默认是120秒,最高限制300s。尽管你设置持续时间大于300s,它也只能持续300s
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
BluetoothAdapter
BluetoothDevice
BluetoothSocket
BluetoothServerSocket
BluetoothClass
BluetoothProfile
BluetoothHeadset
BluetoothA2dp
BluetoothProfile.ServiceListener