检测周边蓝牙设备信号,并绘制半径不同的圆表示信号强弱

本应用通过Android系统搜索周边蓝牙设备,并显示设备名称、地址及信号强度(RSSI值),利用不同颜色区分信号强度等级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

周边蓝牙强弱搜索/AndroidManifest.xml












    +<?xml version="1.0" encoding="utf-8"?>
    +<manifestxmlns:android="http://schemas.android.com/apk/res/android"
    +package="com.example.bt_dir"
    +android:versionCode="1"
    +android:versionName="1.0" >
    +
    + <uses-sdk
    +android:minSdkVersion="8"
    +android:targetSdkVersion="19" />
    +
    + <uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
    + <uses-permissionandroid:name="android.permission.BLUETOOTH" />
    +
    + <application
    +android:allowBackup="true"
    +android:icon="@drawable/ic_launcher"
    +android:label="@string/app_name"
    +android:theme="@style/AppTheme" >
    + <activity
    +android:name=".MainActivity"
    +android:label="@string/app_name" >
    + <intent-filter>
    + <actionandroid:name="android.intent.action.MAIN" />
    +
    + <categoryandroid:name="android.intent.category.LAUNCHER" />
    + </intent-filter>
    + </activity>
    + </application>
    +
    +</manifest>



周边蓝牙强弱搜索/res/layout/activity_main.xml
    +<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    +xmlns:tools="http://schemas.android.com/tools"
    +android:id="@+id/LinearLayout1"
    +android:layout_width="fill_parent"
    +android:layout_height="fill_parent"
    +android:orientation="vertical"
    +tools:context="com.example.bt_dir.MainActivity" >
    +
    + <SurfaceView
    +android:id="@+id/surface"
    + android:layout_width="fill_parent"
    + android:layout_height="fill_parent" />
    +</LinearLayout> 
周边蓝牙强弱搜索/res/menu/main.xml

周边蓝牙强弱搜索/res/menu/main.xml

Vie
   
周边蓝牙强弱搜索/res/layout/activity_main.xml



    +<menuxmlns:android="http://schemas.android.com/apk/res/android"
    +xmlns:app="http://schemas.android.com/apk/res-auto"
    +xmlns:tools="http://schemas.android.com/tools"
    +tools:context="com.example.bt_dir.MainActivity" >
    +
    + <item
    +android:id="@+id/action_settings"
    +android:orderInCategory="100"
    +android:title="@string/action_settings"
    +app:showAsAction="never"/>
    +
    +</menu>
    周边蓝牙强弱搜索/src/com/example/bt_dir/MainActivity.java+packagecom.example.bt_dir;
    +
    +importjava.util.Vector;
    +
    +importandroid.support.v7.app.ActionBarActivity;
    +importandroid.bluetooth.BluetoothAdapter;
    +importandroid.bluetooth.BluetoothDevice;
    +importandroid.content.BroadcastReceiver;
    +importandroid.content.Context;
    +importandroid.content.Intent;
    +importandroid.content.IntentFilter;
    +importandroid.graphics.Canvas;
    +importandroid.graphics.Color;
    +importandroid.graphics.Paint;
    +importandroid.graphics.Paint.Style;
    +importandroid.os.Bundle;
    +importandroid.os.Handler;
    +importandroid.os.Message;
    +importandroid.util.Log;
    +importandroid.view.Menu;
    +importandroid.view.MenuItem;
    +importandroid.view.SurfaceHolder;
    +importandroid.view.SurfaceHolder.Callback;
    +importandroid.view.SurfaceView;
    +
    +publicclass MainActivity extendsActionBarActivity implements Callback {
    +
    +private SurfaceView mSurface;
    +private SurfaceHolder mHolder;
    +private BluetoothAdapter mBtAdapter;
    +//private Message msg ;
    +//private Bundle bundle;
    +
    +private Vector<String> mDevicesVector;
    +private Vector<Short> mRSSIVector;
    +private Vector<Paint> mPaint;
    +//消息句柄(线程里无法进行界面更新,所以要把消息从线程里发送出来在消息句柄里进行处理)
    +public Handler myHandler =new Handler() {
    +@Override
    +public void handleMessage(Messagemsg)
    + {
    +Bundle bundle = msg.getData();
    +short now = bundle.getShort("msg");
    +Log.d("onGet",String.valueOf(now));
    +if (msg.what== 0x01)
    + {
    + draw();
    + }
    + doDiscovery();
    + }
    +//画图像
    +private void draw() {
    +Canvas canvas = mHolder.lockCanvas();
    + canvas.drawRGB(0,0, 0);
    +
    +for(int i=mRSSIVector.size()-1;i>=0;i--)
    + {
    + canvas.drawText(i+":"+mDevicesVector.get(i),5, i*10+12, mPaint.get(i));
    + canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2,150+mRSSIVector.get(i), mPaint.get(i)); //画圆圈
    + }
    + mHolder.unlockCanvasAndPost(canvas);// 更新屏幕显示内容
    + mRSSIVector.clear();
    + mDevicesVector.clear();
    + }
    + };
    +
    +@Override
    +protected void onCreate(BundlesavedInstanceState) {
    +super.onCreate(savedInstanceState);
    + setContentView(R.layout.activity_main);
    +
    +//msg = new Message();//消息
    +///bundle = new Bundle();
    +
    + mDevicesVector=newVector<String>();//向量
    + mRSSIVector=newVector<Short>();
    + mPaint=newVector<Paint>();
    +Paint paint0 = new Paint();
    + paint0.setAntiAlias(true);
    + paint0.setStyle(Style.STROKE);
    + paint0.setColor(Color.RED);
    + mPaint.add(paint0);
    +Paint paint1 = new Paint();
    + paint1.setAntiAlias(true);
    + paint1.setStyle(Style.STROKE);
    + paint1.setColor(Color.GREEN);
    + mPaint.add(paint1);
    +Paint paint2 = new Paint();
    + paint2.setAntiAlias(true);
    + paint2.setStyle(Style.STROKE);
    + paint2.setColor(Color.BLUE);
    + mPaint.add(paint2);
    +Paint paint3 = new Paint();
    + paint3.setAntiAlias(true);
    + paint3.setStyle(Style.STROKE);
    + paint3.setColor(Color.YELLOW);
    + mPaint.add(paint3);
    +Paint paint4 = new Paint();
    + paint4.setAntiAlias(true);
    + paint4.setStyle(Style.STROKE);
    + paint4.setColor(Color.WHITE);
    + mPaint.add(paint4);
    +Paint paint5 = new Paint();
    + paint5.setAntiAlias(true);
    + paint5.setStyle(Style.STROKE);
    + paint5.setColor(Color.LTGRAY);
    + mPaint.add(paint5);
    +Paint paint6 = new Paint();
    + paint6.setAntiAlias(true);
    + paint6.setStyle(Style.STROKE);
    + paint6.setColor(Color.CYAN);
    + mPaint.add(paint6);
    +
    + mSurface=(SurfaceView)findViewById(R.id.surface);
    + mHolder= mSurface.getHolder();
    + mHolder.addCallback(this);
    +
    +// Register for broadcasts when a device is discovered
    +IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    +this.registerReceiver(mReceiver, filter);
    +// Register for broadcasts when discovery has finished
    + filter= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    +this.registerReceiver(mReceiver, filter);
    +
    +// Get the local Bluetooth adapter
    + mBtAdapter= BluetoothAdapter.getDefaultAdapter();
    + }
    +
    +// Start device discover with the BluetoothAdapter
    +private void doDiscovery() {
    +// Indicate scanning in the title
    + setProgressBarIndeterminateVisibility(true);
    +
    +// If we're already discovering, stop it
    +if (mBtAdapter.isDiscovering()) {
    + mBtAdapter.cancelDiscovery();
    + }
    +// Request discover from BluetoothAdapter
    + mBtAdapter.startDiscovery();
    + }
    +
    +// The BroadcastReceiver that listens for discovered devices and
    +// changes the title when discovery is finished
    +//【查找蓝牙设备】
    +private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    +@Override
    +public void onReceive(Contextcontext, Intent intent) {
    +Log.d("onReceive","OK");
    +String action = intent.getAction();
    +// When discovery finds a device
    +if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    +// Get the BluetoothDevice object from the Intent
    +BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    + mDevicesVector.add(device.getName()+ "\n"+ device.getAddress());
    +short rssi = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
    + mRSSIVector.add(rssi);
    +Log.d("RSSI",device.getName()+""+String.valueOf(rssi));
    +// When discovery is finished, change the Activity title
    + }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
    + setProgressBarIndeterminateVisibility(false);
    +if (mDevicesVector.size()!= 0) {
    +Message msg = new Message();//消息
    +Bundle bundle = new Bundle();
    + bundle.clear();Log.d("onReceive","1");
    + msg.what= 0x01;//消息类别
    + bundle.putShort("msg",(short)0);Log.d("onReceive","2");
    + msg.setData(bundle);Log.d("onReceive","3");
    + myHandler.sendMessage(msg);Log.d("onReceive","4");
    + }
    + }
    + }
    + };
    +
    +@Override
    +public boolean onCreateOptionsMenu(Menumenu) {
    +// Inflate the menu; this adds items to the action bar if it is present.
    + getMenuInflater().inflate(R.menu.main, menu);
    +return true;
    + }
    +
    +@Override
    +public boolean onOptionsItemSelected(MenuItemitem) {
    +// Handle action bar item clicks here. The action bar will
    +// automatically handle clicks on the Home/Up button, so long
    +// as you specify a parent activity in AndroidManifest.xml.
    +int id = item.getItemId();
    +if (id == R.id.action_settings) {
    + doDiscovery();
    +return true;
    + }
    +return false;
    + }
    +
    +@Override
    +public void surfaceCreated(SurfaceHolderholder) {
    +// TODO Auto-generated method stub
    +
    + }
    +
    +@Override
    +public void surfaceChanged(SurfaceHolderholder, int format, int width,
    +int height) {
    +// TODO Auto-generated method stub
    +
    + }
    +
    +@Override
    +public void surfaceDestroyed(SurfaceHolderholder) {
    +// TODO Auto-generated method stub
    +
    + }
    +}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值