前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android蓝牙测试

android蓝牙测试

作者头像
提莫队长
发布2019-02-21 11:06:13
9400
发布2019-02-21 11:06:13
举报
文章被收录于专栏:刘晓杰刘晓杰

1.添加权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

2.main.xml

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bluetoothtest.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

3.MainActivity.java

代码语言:javascript
复制
package com.example.bluetoothtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    private List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();
    private int REQUEST_ENABLE_BT = 1;

    private ListView listView;
    private ArrayAdapter<String> adapter;

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

        listView = (ListView)findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);

        if (bluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(), "bluetooth not exist!",
                    Toast.LENGTH_SHORT).show();
            finish();
        } else if (!bluetoothAdapter.isEnabled()) {
            // Show a system activity that allows the user to turn on Bluetooth
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENABLE_BT);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 注册接收器
        Intent discoverIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        // 开启搜索看见模式,最长10s
        discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                10);
        startActivity(discoverIntent);

        IntentFilter discoverFilter = new IntentFilter(
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(discover_R, discoverFilter);
        IntentFilter findFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(find_R, findFilter);

        bluetoothAdapter.startDiscovery();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothAdapter != null) {
            bluetoothAdapter.cancelDiscovery();
            bluetoothAdapter.disable();
        }
    }

    // ACTION_DISCOVERY_FINISHED,搜索结束后追加已配对的设备
    // getName()这一步比较漫长,“已配对”那句话要等好一会儿才出现
    BroadcastReceiver discover_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getName();
            unregisterReceiver(find_R);
            unregisterReceiver(this);
        }
    };

    // ACTION_FOUND,这是搜索到的所有蓝牙(未配对+已配对)
    BroadcastReceiver find_R = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            deviceList.add(device);
            adapter.add(device.getName());
        }
    };

    private void getName() {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0){
            for(BluetoothDevice device : pairedDevices){
                deviceList.add(device);
                adapter.add(device.getName()+"已配对!");
            }
        }
    }
}

注意点:在执行getBondedDevices时候会花费一点时间,一开始我不知道,“已配对”一直没出现,以为出错了。后来上了趟厕所回来发现就有了!!!!尴尬!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年04月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.添加权限
  • 2.main.xml
  • 3.MainActivity.java
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档