android BlueTooth蓝牙
16lz
2021-12-04
阅读更多 MainActivity.java
activity_main.xml
AndroidManifest.xml
package com.blueTooth;import java.util.ArrayList;import java.util.List;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.view.Menu;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.TextView;import android.widget.Toast;import com.blueTooth.R.id;public class MainActivity extends Activity {Listdevices=new ArrayList ();BluetoothDevice device;BluetoothAdapter adapter;private CheckBox checkBox;private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox=(CheckBox) findViewById(R.id.checkBox1); textView=(TextView) findViewById(id.textView1); adapter=BluetoothAdapter.getDefaultAdapter(); if(adapter==null){ checkBox.setEnabled(false); checkBox.setChecked(false); textView.setText("本地无蓝牙设备"); }else{ checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked==false){adapter.disable();textView.setText("未打开蓝牙");}else{adapter.enable();IntentFilter findintent=new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(finreciver,findintent);adapter.startDiscovery();if(devices.size()>0){textView.setText("列表有数据");}else{textView.setText("未发现远程蓝牙设备(列表为空)");}}}}); } } private BroadcastReceiver finreciver=new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString action=intent.getAction();if(action.equals(BluetoothDevice.ACTION_FOUND)){BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);devices.add(device);}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){Toast.makeText(MainActivity.this, "搜索完成",Toast.LENGTH_SHORT).show();System.out.println("搜索完成");}}};}
activity_main.xml
AndroidManifest.xml
- com.blueTooth.rar (688.3 KB)
- 下载次数: 0
更多相关文章
- 开篇
- Android(安卓)获取设备各种信息以及其它
- qt部署到android设备,很久没响应解决办法
- Notes on the implementation of encryption in Android(安卓)3.
- Android(安卓)获取设备各种信息以及其它
- 优雅的处理Android数据库升级的问题
- Android(安卓)Usb源码分析
- JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器
- android adb常用命令整理