【安卓蓝牙应用】


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


一、蓝牙

蓝牙,是一种支持设备短距离通信(一般10m内)的无线电技术,能在包括移动电话、PDA、无线耳机、笔记本电脑、相关外设等众多设备之间进行无线信息交换。利用“蓝牙”技术,能够有效地简化移动通信终端设备之间的通信,也能够成功地简化设备与因特网Internet之间的通信,从而数据传输变得更加迅速高效,为无线通信拓宽道路。

二、Android蓝牙开发

在Android蓝牙开发中,主要用到BluetoothManage和BluetoothAdapter这两个类,从名字上看一个是蓝牙管理器,一个是蓝牙适配器。下面来看下怎么使用:

1.开发蓝牙相关的APP,首先要在清单文件中声明:

代码如下(示例):

  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <!-- android5.0以上需要定位权限 -->
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
   />
2.BluetoothManager和BluetoothAdapter

代码如下(示例):

//BluetoothManager通过getSystemService获取
var bluetoothManager:BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
// BluetoothAdapter可以通过BluetoothManager获取,也可以通过BluetoothAdapter获取
var adapter = bluetoothManager.adapter
adapter = BluetoothAdapter.getDefaultAdapter();
3.判断是否支持蓝牙
var bluetoothManager:BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
var adapter = bluetoothManager.adapter
if (adapter == null) {//如果adapter为空,不支持蓝牙
    Toast.makeText(this,"Bluetooth is not supported by the device",Toast.LENGTH_SHORT).show()
    return
}
4.打开蓝牙
class MainActivity : AppCompatActivity() {

    val REQUEST_ENABLE_BT = 2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var bluetoothManager:BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        var adapter = bluetoothManager.adapter
        if (adapter == null) {
            Toast.makeText(this, "Bluetooth is not supported by the device", Toast.LENGTH_SHORT)
                .show()
            return
        } else {
            if (!adapter.isEnabled) {//未开启
                var enableIntent: Intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT)
            } else {
                //TODO 蓝牙开启
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == Activity.RESULT_OK) {
                //TODO 蓝牙开启
            } else {
                // bluetooth is not open
                Toast.makeText(
                    this, "蓝牙没有开启",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
}

5.搜索蓝牙设备

搜索蓝牙设备,需要注册个广播接收者,然后系统搜索到蓝牙设备的时候,系统会发送广播通知我们。

class MainActivity : AppCompatActivity() {

    val REQUEST_ENABLE_BT = 2

    private var mDevicesArrayAdapter: ArrayList<String> = ArrayList()

    val mFindBlueToothReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            var action = intent.action
            if (BluetoothDevice.ACTION_FOUND == action) {
                // 找到新的蓝牙设备
                var device : BluetoothDevice? =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                if (device?.bondState != BluetoothDevice.BOND_BONDED) {
                    mDevicesArrayAdapter.add(device?.name + "\n" + device?.address + "\n" + "未配对")
                }
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) {
                // 扫描完成
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var bluetoothManager:BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        var adapter = bluetoothManager.adapter
        if (adapter == null) {
            Toast.makeText(this, "Bluetooth is not supported by the device", Toast.LENGTH_SHORT)
                .show()
            return
        } else {
            if (!adapter.isEnabled) {//未开启
                var enableIntent: Intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT)
            } else {
                //TODO 蓝牙开启
                getDeviceList()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_ENABLE_BT) {
            if (resultCode == Activity.RESULT_OK) {
                //TODO 蓝牙开启
                getDeviceList()
            } else {
                // bluetooth is not open
                Toast.makeText(
                    this, "蓝牙没有开启",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    private fun getDeviceList() {
        // Register for broadcasts when a device is discovered
        var filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
        this.registerReceiver(mFindBlueToothReceiver, filter)
        // Register for broadcasts when discovery has finished
        filter = IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
        this.registerReceiver(mFindBlueToothReceiver, filter)
        var adapter = BluetoothAdapter.getDefaultAdapter()
        val bondedDevices: Set<BluetoothDevice> = adapter.bondedDevices //获取已配对蓝牙列表
        if (bondedDevices.size > 0) {
            for (device in bondedDevices) {
                mDevicesArrayAdapter.add(device.name + "\n" + device.address + "\n" + "已配对")
            }
        }
        adapter.startDiscovery() // 开始扫描
    }
}
6.配对
val remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()) //这里传入蓝牙的mac地址
var method: Method? = null
try {
    if (remoteDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
        method = BluetoothDevice::class.java.getMethod("createBond")
        Log.e("log", "开始配对")
        method.invoke(remoteDevice)
    }
} catch (e: Exception) {
    e.printStackTrace()
}

7.连接打印机
Thread(Runnable {
    val remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()) //这里传入蓝牙的mac地址
    var method: Method? = null
    try {
        if (remoteDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
            method = BluetoothDevice::class.java.getMethod("createBond")
            Log.e("log", "开始配对")
            method.invoke(remoteDevice)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    var socket: BluetoothSocket? = null
    try {
        // 蓝牙串口服务对应的UUID。如使用的是其它蓝牙服务,需更改下面的字符串
        if (BluetoothAdapter.checkBluetoothAddress(remoteDevice.getAddress())) {
            val MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
            socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID)
            val connected = socket!!.isConnected
            try {
                if (!connected) {
                    socket.connect()
                }
            } catch (e: Exception) {
                Log.i("log", "关闭重连")
                socket.close()
                socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID)
                if (!connected) {
                    socket!!.connect()
                }
                Log.i("log", "重连失败")
            }

            BluetoothUtils.setBluetoothSocket(socket)
            Log.d("log", "连接成功")
            runOnUiThread {
                Toast.makeText(applicationContext, "连接成功", Toast.LENGTH_SHORT).show()
                finish()
            }

        }

    } catch (e: Exception) {
        try {
            if (socket != null) {
                socket.close()
                socket = null
            }
        } catch (e1: Exception) {
            e.printStackTrace()
        }

        e.printStackTrace()
        Log.d("log", "获取Socket失败")
        runOnUiThread {
            Toast.makeText(applicationContext, "连接失败", Toast.LENGTH_SHORT).show()
        }
    }
}).start()

public class BluetoothUtils {

	private static BluetoothSocket mmSocket = null;
	
	public static void setBluetoothSocket(BluetoothSocket socket) {
		mmSocket = socket;
	}
	
	public static BluetoothSocket getBluetoothSocket() {
		if(mmSocket != null) {
			return mmSocket;
		}
		return null;
	}
}

获取到BluetootSocket,就可以获取输入输出流

tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();

总结

1.蓝牙开发中,BluetoothManage和BluetoothAdapter是两个主要的对象
2.搜索蓝牙设备,需要注册个广播接收者,然后系统搜索到蓝牙设备的时候,系统会发送广播通知我们。
3.BluetoothAdapter可以通过BluetoothManager获取,也可以通过BluetoothAdapter获取

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值