智能家居

public class BlueToothActivity extends Activity implements OnClickListener,OnItemClickListener {

   private ListView lv_bluetooth_devices;
   private BluetoothAdapter bluetoothAdapter;
   private MyBlueToothReceiver myBlueToothReceiver;
   private ArrayList<BluetoothDevice> arrayList;
   private MyBlueToothAdapter myBlueToothAdapter;
   private OutputStream outputStream;
   private class MyBlueToothReceiver extends BroadcastReceiver{

      @Override
      public void onReceive(Context context, Intent intent) {
         if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction())) {
            Toast.makeText(getApplicationContext(), "开始扫描", Toast.LENGTH_SHORT).show();//开始扫描的广播
         }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())){
            Toast.makeText(getApplicationContext(), "扫描完成", Toast.LENGTH_SHORT).show();//扫描完成的广播
         }else if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction())){
            //找到一个可用蓝牙设备的广播
            //获取出蓝牙设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(arrayList == null) arrayList = new ArrayList<BluetoothDevice>();
            arrayList.add(device);//将蓝牙设备添加到集合
            if (myBlueToothAdapter == null) {
               myBlueToothAdapter = new MyBlueToothAdapter(BlueToothActivity.this, arrayList);
            }
            //给listview设置adapter
            if (lv_bluetooth_devices.getAdapter() == null) {//没有设置adapter
               lv_bluetooth_devices.setAdapter(myBlueToothAdapter);
            }else{//设置够adapter
               myBlueToothAdapter.setData(arrayList);//设置adapter数据
               myBlueToothAdapter.notifyDataSetChanged();//更新adapter
            }
         }
      }
   }
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_blue_tooth);

      myBlueToothReceiver = new MyBlueToothReceiver();
      IntentFilter intentFilter = new IntentFilter();
      intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始扫描的广播
      intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//扫描完成的广播事件
      intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//找到可用的蓝牙设备的广播事件
      registerReceiver(myBlueToothReceiver, intentFilter);

      init();
   }

   /**
    * 初始化控件
    */
   private void init() {
      findViewById(R.id.btn_open_bluetooth).setOnClickListener(this);
      findViewById(R.id.btn_close_bluetooth).setOnClickListener(this);

      findViewById(R.id.btn_start_discovery).setOnClickListener(this);
      findViewById(R.id.btn_close_discovery).setOnClickListener(this);

      findViewById(R.id.btn_open_light).setOnClickListener(this);
      findViewById(R.id.btn_close_light).setOnClickListener(this);
      findViewById(R.id.btn_open_once).setOnClickListener(this);

      lv_bluetooth_devices = (ListView) findViewById(R.id.lv_bluetooth_devices);
      lv_bluetooth_devices.setOnItemClickListener(this);
      //获取蓝牙适配器
      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   }

   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.btn_open_bluetooth:
            if (bluetoothAdapter.isEnabled()) return;
            bluetoothAdapter.enable();//打开蓝牙
            break;
         case R.id.btn_close_bluetooth:
            if(!bluetoothAdapter.isEnabled()) return;
            bluetoothAdapter.disable();//关闭蓝牙
            break;
         case R.id.btn_start_discovery:
            bluetoothAdapter.startDiscovery();//扫描蓝牙
            if (arrayList != null) {
               arrayList.clear();
               myBlueToothAdapter.setData(arrayList);
               myBlueToothAdapter.notifyDataSetChanged();
            }
            break;
         case R.id.btn_close_discovery:
            bluetoothAdapter.cancelDiscovery();//停止扫描
            break;
         case R.id.btn_open_light:
            executeBlueTooth(1);
            break;
         case R.id.btn_close_light:
            executeBlueTooth(2);
            break;
         case R.id.btn_open_once:
            executeBlueTooth(3);
            break;
      }
   }
   private void executeBlueTooth(int type){
      if(outputStream == null) return;
      try {
         byte[] b = new byte[5];
         b[0] = (byte)0x01;//1
         b[1] = (byte)0x99;//-103
         if (type ==1 ) {//开灯
            b[2] = (byte)0x10;//16
            b[3] = (byte)0x10;
         }else if(type == 2){//关灯
            b[2] = (byte)0x11;//17
            b[3] = (byte)0x11;
         }else if(type == 3){//点动
            b[2] = (byte)0x19;//25
            b[3] = (byte)0x19;
         }
         b[4] = (byte)0x99;//-103
         outputStream.write(b);
         outputStream.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
                     long id) {
      //获取出选中的蓝牙
      BluetoothDevice bluetoothDevice = arrayList.get(position);
      //连接蓝牙
      connectBlueTooth(bluetoothDevice);
   }
   /**
    * 连接蓝牙
    * @param bluetoothDevice
    */
   private void connectBlueTooth(final BluetoothDevice bluetoothDevice) {
      new Thread(){
         public void run() {
            try {
               BluetoothSocket bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
               bluetoothSocket.connect();//连接的操作
               outputStream = bluetoothSocket.getOutputStream();//获取发送指令的操作
               Looper.prepare();
               Toast.makeText(getApplicationContext(), "连接成功", Toast.LENGTH_SHORT).show();
               Looper.loop();
            } catch (IOException e) {
               e.printStackTrace();
            }
         };
      }.start();
   }
   @Override
   protected void onDestroy() {
      super.onDestroy();
      unregisterReceiver(myBlueToothReceiver);
   }
}


public class MyBlueToothAdapter extends BaseAdapter {

   private Context mContext;
   private ArrayList<BluetoothDevice> mList;
   private LayoutInflater mInflater;
   
   public MyBlueToothAdapter(Context context,ArrayList<BluetoothDevice> list){
      this.mContext = context;
      this.mList = list;
      this.mInflater = LayoutInflater.from(mContext);
   }
   @Override
   public int getCount() {
      // TODO Auto-generated method stub
      return mList.size();
   }

   @Override
   public Object getItem(int position) {
      // TODO Auto-generated method stub
      return mList.get(position);
   }

   @Override
   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      View view ;
      if (convertView == null) {
         view = mInflater.inflate(R.layout.item_bluetooth, null);
      }else{
         view = convertView;
      }
      TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);
      TextView tv_item_address = (TextView) view.findViewById(R.id.tv_item_address);
      BluetoothDevice bluetoothDevice = mList.get(position);
      tv_item_name.setText(bluetoothDevice.getName());//������������
      tv_item_address.setText(bluetoothDevice.getAddress());//����������ַ
      return view;
   }
   public void setData(ArrayList<BluetoothDevice> list){
      this.mList = list;
   }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值