发布网友
发布时间:2022-04-23 17:20
共1个回答
热心网友
时间:2023-10-17 14:22
首先要操作蓝牙先要AndroidManifest.xml加入权限
看apiAndroid所关于蓝牙发类都android.bluetooth包 需要用几已:
1.BluetoothAdapter 顾名思义蓝牙适配器直我建立bluetoothSocket连接前都要断操作BluetoothAdapter用几:cancelDiscovery() 根据字面意思取消发现说搜索设备候调用再继续搜索disable()关闭蓝牙enable()打蓝牙打蓝牙弹提示更候需要问用户否打两行代码同打蓝牙提示用户:Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);
getAddress()获取本蓝牙址getDefaultAdapter()获取默认BluetoothAdapter实际种获取BluetoothAdaptergetName()获取本蓝牙名称getRemoteDevice(String address)根据蓝牙址获取远程蓝牙设备getState()获取本蓝牙适配器前状态(觉能调试候更需要)isDiscovering()判断前否查找设备返true***isEnabled()判断蓝牙否打已打返true否则返false***listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称UUID创建并返BluetoothServerSocket创建BluetoothSocket服务器端第步startDiscovery()始搜索搜索第步 2.BluetoothDevice看名字知道类描述蓝牙设备createRfcommSocketToServiceRecord(UUIDuuid)根据UUID创建并返BluetoothSocket我获取BluetoothDevice目——创建BluetoothSocket
类其getAddress(),getName(),同BluetoothAdapter;
3.BluetoothServerSocket除Bluetooth相信家定再熟悉既Socket应该都差类种三
两重载accept(),accept(inttimeout)两者区别于面指定间需要注意执行两候直接收客户端请求(或期)都阻塞线程应该放新线程运行
点需要注意两都返BluetoothSocket连接服务器端与客户端两BluetoothSocket连接
4.BluetoothSocket,跟BluetoothServerSocket相客户端共5意外都用close(),关闭connect()连接getInptuStream()获取输入流getOutputStream()获取输流getRemoteDevice()获取远程设备指获取bluetoothSocket指定连接远程蓝牙设备