如何模仿安卓手机中的蓝牙列表?

loie 发布于 2014/09/05 22:11
阅读 626
收藏 0
package com.myb.bluetooth;

import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class getBlueToothList extends Activity {
    private TextView mTextView;
    private ListView lv;
    public ArrayAdapter<String> adapter;
    private BluetoothAdapter mBluetoothAdapter;
    public String  name;
    public class MReceiver extends BroadcastReceiver{
        public String name;
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // 获得已经搜索到的蓝牙设备  
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 搜索到的不是已经绑定的蓝牙设备  
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    // 显示在TextView上  
                  /*mTextView.append(device.getName() + "\t"
                            + device.getAddress()+"\t"+"未绑定"+"\n"); */

   问题出现在这里这里想用列表的形式显示出来,每个条目可以单独相应单击事件。但是adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);这句话只能用在Oncreat中

                }
                // 搜索完成  
            } else if (action
                    .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle("搜索蓝牙设备");
            }
        }
        
    }
    
    
  /*  private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // 获得已经搜索到的蓝牙设备  
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 搜索到的不是已经绑定的蓝牙设备  
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    // 显示在TextView上  
                  mTextView.append(device.getName() + "\t"
                            + device.getAddress()+"\t"+"未绑定"+"\n");
                }
                // 搜索完成  
            } else if (action
                    .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle("搜索蓝牙设备");
            }
        }
    }; */
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 
        setContentView(R.layout.activity_search);
       mTextView = (TextView) findViewById(R.id.tvDevices);
       MReceiver mReceiver=new MReceiver();
       adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
       lv= (ListView)findViewById(R.id.listView1);
       lv.setAdapter(adapter);
       adapter.add("123");
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 获取所有已经绑定的蓝牙设备  
       /* Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
        if (devices.size() > 0) {
            for (BluetoothDevice bluetoothDevice : devices) {
            /*    mTextView.append(bluetoothDevice.getName() + ":" +"\t"

                        + bluetoothDevice.getAddress() + "\t"+"已绑定"+"\n\n"); */


            }
        } */
        // 注册用以接收到已搜索到的蓝牙设备的receiver  
        IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, mFilter);
        // 注册搜索完时的receiver  
        mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, mFilter);
    }
 
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub  
        super.onDestroy();
        //解除注册  
        MReceiver mReceiver=new MReceiver();
        unregisterReceiver(mReceiver);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    public void onClick_Search(View v) {
        setProgressBarIndeterminateVisibility(true);
        setTitle("正在扫描....");
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        // 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回  
        mBluetoothAdapter.startDiscovery();
    }
    
 
}


上面是我自己写的一些代码,现在问题是在Onreceiver中无法使用ListView!

加载中
0
吴笑笑
你直接绑定就好了吧,然后用代码动态添加View吧,蓝牙4.0BLE开发官网有demo,楼主可以自己去下载看看
吴笑笑
回复 @loie : BLE中没有必要通过广播来通知。。。貌似楼主开发的蓝牙是不是BLE。。。表示没有经验
吴笑笑
回复 @loie : 我感觉你是想要扫描设备后把设备显示出来,就是说你先绑定一个adapter 然后获取到设备了就有callback被调用,在回调事件中更新mLeDeviceListAdapter.notifyDataSetChanged();以及其他操作,,,以上是针对BLE编程
l
loie
不是很明白?能帮忙解释清楚点吗?
0
Kinegratii
Kinegratii

直接在广播接收中使用


adapter.add(device.getName());  //

android的适配器是MVC模式的,



//设置一个适配器
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); 
lv= (ListView)findViewById(R.id.listView1); 
//绑定
lv.setAdapter(adapter); 

//之后使用adapter的方法就可以实现ListView数据的更新





返回顶部
顶部