蓝牙原理Android代码实现
本文实例为大家分享了Android实现蓝牙原理代码,供大家参考,具体内容如下
packagecom.example.se7en.testbluetooth; importandroid.app.Activity; importandroid.bluetooth.BluetoothAdapter; importandroid.bluetooth.BluetoothDevice; importandroid.bluetooth.BluetoothServerSocket; importandroid.bluetooth.BluetoothSocket; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.View; importandroid.widget.AdapterView; importandroid.widget.EditText; importandroid.widget.ListView; importandroid.widget.Toast; importjava.io.IOException; importjava.util.ArrayList; importjava.util.List; importjava.util.UUID; publicclassMainActivityextendsActivity { privateBluetoothAdapteradapter; privateMyReceiverreceiver; privateListViewmListView; privateList<BluetoothDevice>mDevices; privatecom.example.se7en.testbluetooth.DeviceAdaptermDeviceAdapter; privateBluetoothSocketmSocket; privateEditTextet; @Override protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView=(ListView)findViewById(R.id.lv); et=(EditText)findViewById(R.id.et); adapter=BluetoothAdapter.getDefaultAdapter(); BlueToothHandler.getInstance().setContext(this); init(); } privatevoidinit() { initListView(); //判断是否支持蓝牙功能 if(adapter==null) { Toast.makeText(this,"该手机不支持蓝牙功能",Toast.LENGTH_LONG).show(); return; } //判断蓝牙功能是否打开 if(!adapter.isEnabled()) { //强行打开 //adapter.enable(); Intentintent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); } IntentFilterfilter=newIntentFilter(); //注册扫描开始的广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //注册扫描结束的广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //注册扫描已经找到设备的广播 filter.addAction(BluetoothDevice.ACTION_FOUND); receiver=newMyReceiver(); registerReceiver(receiver,filter); startBlueServer(); } /** *启动蓝牙服务 */ privatevoidstartBlueServer() { newThread() { @Override publicvoidrun() { while(true) { try { BluetoothServerSocketserverSocket=adapter .listenUsingRfcommWithServiceRecord( "蓝牙服务", UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6")); mSocket=serverSocket.accept();//阻塞 BlueToothHandler.getInstance().setSocket(mSocket); } catch(IOExceptione) { e.printStackTrace(); } } } }.start(); } publicvoiddoClick(Viewview) { switch(view.getId()) { caseR.id.btn: mDevices.clear(); mDeviceAdapter.notifyDataSetChanged(); //判断是否正在扫描,如果没在扫描那么就开始扫描 if(!adapter.isDiscovering()&&adapter.startDiscovery()) { Toast.makeText(this,"开始扫描周围的蓝牙设备",Toast.LENGTH_LONG) .show(); } break; caseR.id.send: if("".equals(et.getText().toString())) { Toast.makeText(this,"内容不能为空",Toast.LENGTH_SHORT).show(); return; } if(BlueToothHandler.getInstance().getSocket()==null) { Toast.makeText(this,"还未连接设备",Toast.LENGTH_SHORT).show(); return; } newThread() { @Override publicvoidrun() { BlueToothHandler.getInstance().sendMessage( et.getText().toString()); } }.start(); break; caseR.id.dis: BlueToothHandler.getInstance().closeSocket(); break; } } @Override protectedvoidonDestroy() { super.onDestroy(); if(receiver!=null) { //注销广播 unregisterReceiver(receiver); } } privatevoidinitListView() { mDevices=newArrayList<BluetoothDevice>(); mDeviceAdapter=newcom.example.se7en.testbluetooth.DeviceAdapter(this,mDevices); mListView.setAdapter(mDeviceAdapter); mListView.setOnItemClickListener(newAdapterView.OnItemClickListener() { @Override publicvoidonItemClick(AdapterView<?>parent,Viewview, intposition,longid) { try { mSocket=mDevices .get(position) .createRfcommSocketToServiceRecord( UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6")); mSocket.connect();//阻塞 Toast.makeText(MainActivity.this,"连接成功", Toast.LENGTH_SHORT).show(); BlueToothHandler.getInstance().setSocket(mSocket); } catch(IOExceptione) { e.printStackTrace(); } } }); } publicclassMyReceiverextendsBroadcastReceiver { @Override publicvoidonReceive(Contextcontext,Intentintent) { Stringaction=intent.getAction(); if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { Log.i("info","开始扫描"); } elseif(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { Log.i("info","扫描结束"); } elseif(action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevicedevice=intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("info",device.getName()+":"+device.getAddress()); if(mDevices!=null) { for(inti=0;i<mDevices.size();i++) { if(device.getAddress() .equals(mDevices.get(i).getAddress())) { return; } } mDevices.add(device); mDeviceAdapter.notifyDataSetChanged(); } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。