【App】蓝牙Bluetooth,app的编写总结

news/2024/7/6 6:29:31
蓝牙四大必须任务:
	1.设置蓝牙
	2.查找局部区域内的配对设备,或可用设备
	3.连接设备
	4.再设备之间传输数据
	
蓝牙权限:
	BLUETOOTH,
	ACCESS_FINE_LOCATION		//android 9, API 28
	ACCESS_COARSE_LOCATION		// < android9

借助远程设备的已知 MAC 地址,您可以随时向其发起连接,而无需执行发现操作	
	

 

是先看了别人写的测试apk,蓝牙wifi测试这些,然后发现别人写的思路完全就是套路。哈哈。

程序基本步骤就是google开发者这个网站上写的。
     https://developer.android.google.cn/guide/topics/connectivity/bluetooth

然后自己也敲了一遍代码,然后提取一些记录一下。

步骤:
    
 /*********************************
  *
  *        开启蓝牙的两种方式
  *
  * *********************************/


1.
    if (!mBluetoothAdapter.isEnabled()) {

            mBluetoothAdapter.enable();

        }

2. 
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
         /*********************************
         *
         *        step 1, 开启蓝牙,获得适配器
         *
         * *********************************/
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            Log.d("QYC",  "Device doesn't support Bluetooth");
        }
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
        }

        /*********************************
         *
         *        step2,检查已连接过的设备
         *
         * *********************************/
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            // There are paired devices. Get the name and address of each paired device.
            for (BluetoothDevice device : pairedDevices) {
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                Log.d("QYC", "qyc1, deviceName == " + deviceName);
                Log.d("QYC", "qyc1, deviceHardwareAddress == " + deviceHardwareAddress);
            }
        }

        /*********************************
         *
         *        step3,注册广播接收者,发现新设备
         *
         * *********************************/
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(receiver, filter);

        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
        bluetoothAdapter.startDiscovery();

    }//end onCreate

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d("QYC", "qyc, action == " + action);
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                Log.d("QYC", "qyc, deviceName == " + deviceName);
                Log.d("QYC", "qyc, deviceHardwareAddress == " + deviceHardwareAddress);
            }
        }
    };

蓝牙的配对方式:

             https://blog.csdn.net/u010783226/article/details/103162894?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161605394816780265469869%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161605394816780265469869&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-103162894.pc_search_result_before_js&utm_term=%E8%93%9D%E7%89%99%E9%85%8D%E5%AF%B9

通过反射调用进行配对:

             https://blog.csdn.net/u010783226/article/details/103162894?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161605394816780265469869%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161605394816780265469869&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-103162894.pc_search_result_before_js&utm_term=%E8%93%9D%E7%89%99%E9%85%8D%E5%AF%B9

蓝牙配对方式:

            https://blog.csdn.net/u010783226/article/details/103162894?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161605394816780265469869%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161605394816780265469869&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-103162894.pc_search_result_before_js&utm_term=%E8%93%9D%E7%89%99%E9%85%8D%E5%AF%B9


http://www.niftyadmin.cn/n/3876804.html

相关文章

微信小程序selectable=‘true‘无复制效果解决

<text selectable></text>或<text selectable{{true}}></text>

20个高效正则表达式

2019独角兽企业重金招聘Python工程师标准>>> 1 . 校验密码强度 密码的强度必须是包含大小写字母和数字的组合&#xff0c;不能使用特殊字符&#xff0c;长度在8-10之间。 ^(?.*\\d)(?.*[a-z])(?.*[A-Z]).{8,10}$ 2. 校验中文 字符串仅能是中文。 ^[\\u4e00-\\u9f…

【androidstudio】下载gradle和sdk慢,更新不了的问题

下不下来&#xff0c;网络那些问题的解决 改hosts文件vim /etc/hosts#qyc add 2020/10/19 google app engine 203.208.41.32 appengine.google.com 203.208.50.167 dl.google.com 203.208.50.167 dl-ssl.google.com 这个快速的IP是怎么知道的&#xff1a; 这个IP是怎么发现…

【App】listview的记录

直接接使用ListView组件创建列表视图&#xff0c;可以有两种方式。一种是通过在XML布局文件中使用<ListView>标记添加&#xff0c;android:entries"array/ctype"<string-array name"type"><item>AAA</item><item>BBB</it…

【Android驱动】pinctrl控制gpio写法

自己也简单总结下&#xff0c;方便下次写 1.头文件#include <linux/pinctrl/consumer.h>2.四个指针struct pinctrl* pintrl;struct pinctrl_state *pins_default; //"default"标签的gpio&#xff0c;在驱动probe之前会设置成default对应的gpio状态//pinc…

[转]用GSON 五招之内搞定任何JSON数组

关于GSON的入门级使用&#xff0c;这里就不提了&#xff0c;如有需要可以看这篇博文 《Google Gson的使用方法,实现Json结构的相互转换》 &#xff0c;写的很好&#xff0c;通俗易懂。 我为什么写这篇文章呢&#xff1f;因为前几晚跟好友 xiasuhuei321 探讨了一下GSON解析复杂的…

【Android驱动】 spi0测试

背景&#xff1a;msm8909平台&#xff0c;c文件测试spi_0的一次记录 测试文件就是写个c文件&#xff0c;编译进内核&#xff0c;引用dtsi里的spi_0&#xff0c;主要probe里面这个函数&#xff0c;进行的SPI的环测试&#xff1a;static int spi_test_transfer(struct spi_devic…

I2S的记录

先记着&#xff0c;后面再加 介绍&#xff1a; https://www.pianshen.com/article/6461715634/