android 藍牙搜索

jopen 9年前發布 | 1K 次閱讀 Java Android

1.搜索藍牙權限

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

2.MainActivity.java

package com.example.bluetooth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ListView tvBlueName;
    private Map<String, String> map;
    private List<Map<String,String>> list;
    private DeviceAdapter adapter;
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvBlueName = (ListView) findViewById(R.id.textView1);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        //獲取已經綁定的藍牙設備信息
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
        list = new ArrayList<Map<String,String>>();
        if(devices.size() > 0){
            for(BluetoothDevice bluetoothDevice : devices){
                map = new HashMap<String, String>();
                map.put("name", bluetoothDevice.getName());
                map.put("address", bluetoothDevice.getAddress());
                list.add(map);
            }
            adapter = new DeviceAdapter();
            tvBlueName.setAdapter(adapter);
        }

        // 注冊用以接收到已搜索到的藍牙設備的receiver  
        IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
        registerReceiver(mReceiver, mFilter);  

        // 注冊搜索完時的receiver  
        mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);  
        registerReceiver(mReceiver, mFilter);  


        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setProgressBarIndeterminateVisibility(true);  
                setTitle("正在掃描....");  
                // 如果正在搜索,就先取消搜索  
                if (mBluetoothAdapter.isDiscovering()) {  
                    mBluetoothAdapter.cancelDiscovery();  
                }  
                // 開始搜索藍牙設備,搜索到的藍牙設備通過廣播返回  
                mBluetoothAdapter.startDiscovery();  
            }
        });
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {  

        @Override  
        public void onReceive(Context context, Intent intent) {  
            String action = intent.getAction();  
            if(action.equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //獲取未綁定的藍牙設備
                if(device.getBondState() != BluetoothDevice.BOND_BONDED){
                    map = new HashMap<String, String>();
                    map.put("name", device.getName());
                    map.put("address", device.getAddress());
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
            }else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {  
                setProgressBarIndeterminateVisibility(false);  
                setTitle("搜索藍牙設備");  
            } 
        }  
    };  

    class DeviceAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = getLayoutInflater().inflate(R.layout.item,null);
            TextView tv1 = (TextView) v.findViewById(R.id.name);
            TextView tv2 = (TextView) v.findViewById(R.id.address);
            tv1.setText(list.get(position).get("name"));
            tv2.setText(list.get(position).get("address"));
            return v;
        }
    }
}

3.activity_main.xml

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="搜索" />

    <ListView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="10dp" />

</RelativeLayout>


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!