Android:BT测试代码
16lz
2021-01-23
源代码:
main.xml:[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:orientation="vertical">
- <TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:text="TextView"android:layout_height="wrap_content"></TextView>
- <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout1">
- <Buttonandroid:id="@+id/button1"android:text="OFF"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
- </LinearLayout>
- <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout2">
- <Buttonandroid:id="@+id/button2"android:text="开启可见"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
- <TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="设备不可见"></TextView>
- </LinearLayout>
- <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/linearLayout3">
- <Buttonandroid:id="@+id/button3"android:text="扫描:OFF"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
- <TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="停止扫描"></TextView>
- </LinearLayout>
- <ListViewandroid:id="@+id/listView1"android:layout_height="wrap_content"android:layout_width="match_parent"></ListView>
- </LinearLayout>
test_bluetooth.java:
[java] view plain copy print ?
- packagecom.test_bluetooth;
- importjava.util.Set;
- importandroid.app.Activity;
- importandroid.bluetooth.BluetoothAdapter;
- importandroid.bluetooth.BluetoothDevice;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.IntentFilter;
- importandroid.os.Bundle;
- importandroid.os.CountDownTimer;
- importandroid.view.View;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.Button;
- importandroid.widget.ListView;
- importandroid.widget.TextView;
- publicclasstest_bluetoothextendsActivityimplementsView.OnClickListener
- {
- privatestaticfinalintREQUEST_ENABLE_BT=2;
- TextViewtxt;
- TextViewtxt_see;
- TextViewtxt_scan;
- BluetoothAdaptermBluetoothAdapter;
- ArrayAdapter<String>mArrayAdapter;
- Buttonbtn_switch;
- Buttonbtn_see;
- Buttonbtn_scan;
- ListViewlist;
- CountDownTimersee_timer;
- CountDownTimerscan_timer;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txt=(TextView)findViewById(R.id.textView1);
- txt_see=(TextView)findViewById(R.id.textView2);
- txt_scan=(TextView)findViewById(R.id.textView3);
- //绑定XML中的ListView,作为Item的容器
- list=(ListView)findViewById(R.id.listView1);
- //获取蓝牙适配器
- mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
- mArrayAdapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
- if(mBluetoothAdapter==null)
- {
- //DevicedoesnotsupportBluetooth
- txt.setText("fail");
- //退出程序
- test_bluetooth.this.finish();
- }
- btn_switch=(Button)findViewById(R.id.button1);
- btn_switch.setOnClickListener(this);
- btn_see=(Button)findViewById(R.id.button2);
- btn_see.setOnClickListener(this);
- btn_see.setEnabled(false);
- btn_scan=(Button)findViewById(R.id.button3);
- btn_scan.setOnClickListener(this);
- btn_scan.setText("扫描:OFF");
- btn_scan.setEnabled(false);
- //判断蓝牙是否已经被打开
- if(mBluetoothAdapter.isEnabled())
- {
- //打开
- btn_switch.setText("ON");
- btn_see.setEnabled(true);
- btn_scan.setEnabled(true);
- }
- see_timer=newCountDownTimer(120000,1000)
- {
- @Override
- publicvoidonTick(longmillisUntilFinished)
- {
- txt_see.setText("剩余可见时间"+millisUntilFinished/1000+"秒");
- }
- @Override
- publicvoidonFinish()
- {
- //判断蓝牙是否已经被打开
- if(mBluetoothAdapter.isEnabled())
- {
- btn_see.setEnabled(true);
- txt_see.setText("设备不可见");
- }
- }
- };
- scan_timer=newCountDownTimer(12000,1000)
- {
- @Override
- publicvoidonTick(longmillisUntilFinished)
- {
- txt_scan.setText("剩余扫描时间"+millisUntilFinished/1000+"秒");
- }
- @Override
- publicvoidonFinish()
- {
- //判断蓝牙是否已经被打开
- if(mBluetoothAdapter.isEnabled())
- {
- btn_scan.setEnabled(true);
- //关闭扫描
- mBluetoothAdapter.cancelDiscovery();
- btn_scan.setText("扫描:OFF");
- txt_scan.setText("停止扫描");
- }
- }
- };
- }
- @Override
- protectedvoidonDestroy(){
- super.onDestroy();
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- @Override
- publicvoidonClick(Viewv)
- {
- //TODOAuto-generatedmethodstub
- switch(v.getId())
- {
- caseR.id.button1:
- {
- Stringstr=btn_switch.getText().toString();
- if(str=="OFF")
- {
- if(!mBluetoothAdapter.isEnabled())
- {
- //打开蓝牙
- IntentenableBtIntent=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
- txt.setText("s1");
- btn_see.setEnabled(true);
- btn_scan.setText("扫描:OFF");
- btn_scan.setEnabled(true);
- }
- }
- else
- {
- //关闭蓝牙
- mBluetoothAdapter.disable();
- btn_switch.setText("OFF");
- mArrayAdapter.clear();
- list.setAdapter(mArrayAdapter);
- btn_see.setEnabled(false);
- btn_scan.setEnabled(false);
- }
- break;
- }
- caseR.id.button2:
- {
- //开启可见
- IntentenableBtIntent_See=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- startActivityForResult(enableBtIntent_See,3);
- txt.setText("s1");
- btn_see.setEnabled(false);
- see_timer.start();
- break;
- }
- caseR.id.button3:
- {
- Stringstr=btn_scan.getText().toString();
- if(str=="扫描:OFF")
- {
- txt.setText("s5");
- if(mBluetoothAdapter.isEnabled())
- {
- //开始扫描
- mBluetoothAdapter.startDiscovery();
- txt.setText("s6");
- btn_scan.setText("扫描:ON");
- //CreateaBroadcastReceiverforACTION_FOUND
- finalBroadcastReceivermReceiver=newBroadcastReceiver()
- {
- @Override
- publicvoidonReceive(Contextcontext,Intentintent)
- {
- //TODOAuto-generatedmethodstub
- Stringaction=intent.getAction();
- //Whendiscoveryfindsadevice
- mArrayAdapter.clear();
- if(BluetoothDevice.ACTION_FOUND.equals(action))
- {
- //GettheBluetoothDeviceobjectfromtheIntent
- BluetoothDevicedevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- //AddthenameandaddresstoanarrayadaptertoshowinaListView
- mArrayAdapter.add(device.getName()+":"+device.getAddress());
- }
- list.setAdapter(mArrayAdapter);
- }
- };
- //RegistertheBroadcastReceiver
- IntentFilterfilter=newIntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(mReceiver,filter);//Don'tforgettounregisterduringonDestroy
- scan_timer.start();
- }
- }
- else
- {
- //关闭扫描
- mBluetoothAdapter.cancelDiscovery();
- btn_scan.setText("扫描:OFF");
- scan_timer.cancel();
- txt_scan.setText("停止扫描");
- }
- break;
- }
- default:
- break;
- }
- }
- publicvoidonActivityResult(intrequestCode,intresultCode,Intentdata)
- {
- switch(requestCode)
- {
- caseREQUEST_ENABLE_BT:
- //WhentherequesttoenableBluetoothreturns
- if(resultCode==Activity.RESULT_OK)
- {
- //Bluetoothisnowenabled,sosetupachatsession
- btn_switch.setText("ON");
- txt.setText("s4");
- //获取蓝牙列表
- Set<BluetoothDevice>pairedDevices=mBluetoothAdapter.getBondedDevices();
- mArrayAdapter.clear();
- //Iftherearepaireddevices
- if(pairedDevices.size()>0)
- {
- //txt.setText("s3");
- //Loopthroughpaireddevices
- for(BluetoothDevicedevice:pairedDevices)
- {
- //AddthenameandaddresstoanarrayadaptertoshowinaListView
- mArrayAdapter.add(device.getName()+":"+device.getAddress());
- }
- list.setAdapter(mArrayAdapter);
- }
- }else
- {
- finish();
- }
- }
- }
- }
更多相关文章
- Android TimeLine 时间节点轴的实现
- Android时间工具类 本地转UTC,UTC转本地
- 安卓4.X系统 增加蓝牙接收文件类型
- 【Android】Android蓝牙开发深入解析
- android 传统蓝牙开发 (附示例源码)
- android:sharedUserId="android.uid.system" 使用系统签名+SNTP
- Android ----蓝牙架构
- Android中日期和时间控件的使用