蓝牙设备发现

时间:2019-02-01 19:29:08

标签: android bluetooth

我正在尝试制作一个可以检测安装了该设备的设备周围的设备的应用程序。 应该将连接请求发送到检测到的设备,以获取其蓝牙MAC地址。

因此,首先,我尝试检测周围的设备并在日志中打印有关此设备的信息,但是当我在手机的蓝牙设置中可以看到我的设备时,我什么也没有:

bluetooth settings

这是我的主要活动(从发布开始)



    public class Main extends AppCompatActivity {
        public BluetoothAdapter mBluetoothAdapter;
        private final static int REQUEST_ENABLE_BT = 1;
        private static final String TAG = "MyActivity";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                //
            }
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter);
            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            mBluetoothAdapter.startDiscovery();
        }

        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Log.i(TAG, "Device found: " + device.getName() + "; MAC " + device.getAddress());
                }
            }
        };
    }

下面也不同权限是使用我的应用程序:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION "/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

我希望你能帮助我。

0 个答案:

没有答案
相关问题