蓝牙

添加权限

//静态权限
 <!-- 用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- 用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    //动态权限
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.BLUETOOTH},100);
        }

注册广播

 //注册广播
        Broad broad = new Broad();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(broad,intentFilter);

打开蓝牙

//打开蓝牙
    public void dk(View view) {
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);//请求蓝牙打开
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//允许附近的人进行搜索
        //intent.setAction(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);
        startActivity(intent);
    }

关闭蓝牙

 //关闭蓝牙
    public void gb(View view) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter!=null){
            defaultAdapter.disable();
        }
    }

蓝牙名字

//蓝牙名字
    public void bluename(View view) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter!=null){
            String name = defaultAdapter.getName();
            Toast.makeText(this, ""+name, Toast.LENGTH_SHORT).show();
        }
    }

搜索已配对的蓝牙

 //搜索已配对的蓝牙
    public void select(View view) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter!=null){
            Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
            list.addAll(bondedDevices);
            MyAdapter myAdapter = new MyAdapter(list, this);
            re.setLayoutManager(new LinearLayoutManager(this));
            re.setAdapter(myAdapter);

        }
    }

搜索附近

 //搜索附近
    public void near(View view) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter!=null){
            defaultAdapter.startDiscovery();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值