当前位置:操作系统 > 安卓/Android >>

android蓝牙主动发起配对实例

Java代码 
package cn.madfinger.core; 
 
import java.io.IOException; 
import java.lang.reflect.Method; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 
 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.widget.ToggleButton; 
 
public class BlueToothTestActivity extends Activity { 
    //该UUID表示串口服务 
    //请参考文章<a href="http://wiley.iteye.com/blog/1179417">http://wiley.iteye.com/blog/1179417</a> 
    static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; 
    Button btnSearch, btnDis, btnExit; 
    ToggleButton tbtnSwitch; 
    ListView lvBTDevices; 
    ArrayAdapter<String> adtDevices; 
    List<String> lstDevices = new ArrayList<String>(); 
    BluetoothAdapter btAdapt; 
    public static BluetoothSocket btSocket; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        // Button 设置 
        btnSearch = (Button) this.findViewById(R.id.btnSearch); 
        btnSearch.setOnClickListener(new ClickEvent()); 
        btnExit = (Button) this.findViewById(R.id.btnExit); 
        btnExit.setOnClickListener(new ClickEvent()); 
        btnDis = (Button) this.findViewById(R.id.btnDis); 
        btnDis.setOnClickListener(new ClickEvent()); 
 
        // ToogleButton设置 
        tbtnSwitch = (ToggleButton) this.findViewById(R.id.tbtnSwitch); 
        tbtnSwitch.setOnClickListener(new ClickEvent()); 
 
        // ListView及其数据源 适配器 
        lvBTDevices = (ListView) this.findViewById(R.id.lvDevices); 
        adtDevices = new ArrayAdapter<String>(this, 
                android.R.layout.易做图_list_item_1, lstDevices); 
        lvBTDevices.setAdapter(adtDevices); 
        lvBTDevices.setOnItemClickListener(new ItemClickEvent()); 
 
        btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能 
 
        // ======================================================== 
        // modified by wiley 
        /*
         * if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 读取蓝牙状态并显示
         * tbtnSwitch.setChecked(false); else if (btAdapt.getState() ==
         * BluetoothAdapter.STATE_ON) tbtnSwitch.setChecked(true);
         */ 
        if (btAdapt.isEnabled()) { 
            tbtnSwitch.setChecked(false); 
        } else { 
            tbtnSwitch.setChecked(true); 
        } 
        // ============================================================ 
        // 注册Receiver来获取蓝牙设备相关的结果 
        IntentFilter intent = new IntentFilter(); 
        intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果 
        intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 
        intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); 
        intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
        registerReceiver(searchDevices, intent); 
    } 
 
    private BroadcastReceiver searchDevices = new BroadcastReceiver() { 
 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            Bundle b = intent.getExtras(); 
            Object[] lstName = b.keySet().toArray(); 
 
            // 显示所有收到的消息及其细节 
            for (int i = 0; i < lstName.length; i++) { 
                String keyName = lstName[i].toString(); 
                Log.e(keyName, String.valueOf(b.get(keyName))); 
            } 
            BluetoothDevice device = null; 
            // 搜索设备时,取得设备的MAC地址 
            if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
                device = intent 
                        .

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,