• Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备


    一. 修改本机蓝牙设备的可见性

    扫描周围可用的蓝牙设备

    一.  清单文件AdroidManifest.xml:

    <uses-permission android:name="android.permission.BLUETOOTH"/>

    <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    二. 布局文件: main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    	<TextView  
        	android:layout_width="fill_parent" 
        	android:layout_height="wrap_content" 
        	android:text="@string/hello"
        	/>
        <Button 
        	android:id="@+id/discoverButton"
        	android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
        	android:text="设置可见性"/>
        <Button 
        	android:id="@+id/scanButton"
        	android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
        	android:text="开始扫描"/>
    </LinearLayout>
    

      

    三. MainActivity:

    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
        private Button discoverButton = null;
        private Button scanButton = null;
        private BluetoothAdapter adapter = null;
        private BluetoothReceiver bluetoothReceiver = null;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            adapter = BluetoothAdapter.getDefaultAdapter();
            
            discoverButton = (Button)findViewById(R.id.discoverButton);
            scanButton = (Button)findViewById(R.id.scanButton);
            //修改蓝牙设备的可见性
            discoverButton.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View view) {
                Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    
    //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
    discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
    startActivity(discoverIntent);
                }
            });
            
            scanButton.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
            //开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
                    adapter.startDiscovery();
                }
            });
            
            //设定广播接收的filter
            IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            //创建蓝牙广播信息的receiver
            bluetoothReceiver = new BluetoothReceiver ();
            //注册广播接收器
            registerReceiver(bluetoothReceiver,intentFilter);
                
        }
        
        private class BluetoothReceiver extends BroadcastReceiver{
            @Override
            public void onReceive(Context context, Intent intent) {
                //获得扫描到的远程蓝牙设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                System.out.println(device.getAddress());
            }
            
        }
    }
  • 相关阅读:
    C++(OI竞赛入门)学习指南一
    faster-rcnn错误信息 : tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [21] rhs shape= [2]
    ssd训练之bug:Invalid JPEG data or crop window, data size 565248
    coco数据集标注图转为二值图python(附代码)
    静态变量和全局变量的区别
    Tortoisegit常见错误Disconnected no supported authentication methods available(server sent: publickey)
    zookeeper启动为什么占用8080端口,修改哪个配置文件可以改变端口?
    安装Dubbo 并且安装注册中心(Zookeeper-3.3.6)
    Zookeeper基础命令操作
    maven环境变量配置不成功的原因
  • 原文地址:https://www.cnblogs.com/lhj588/p/3610447.html
Copyright © 2020-2023  润新知