栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

蓝牙原理Android代码实现

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝牙原理Android代码实现

本文实例为大家分享了Android实现蓝牙原理代码,供大家参考,具体内容如下

package com.example.se7en.testbluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
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.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MainActivity extends Activity
{

  private BluetoothAdapter adapter;

  private MyReceiver receiver;

  private ListView mListView;

  private List mDevices;

  private com.example.se7en.testbluetooth.DeviceAdapter mDeviceAdapter;

  private BluetoothSocket mSocket;

  private EditText et;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.lv);
    et = (EditText) findViewById(R.id.et);
    adapter = BluetoothAdapter.getDefaultAdapter();
    BlueToothHandler.getInstance().setContext(this);
    init();
  }

  private void init()
  {
    initListView();
    // 判断是否支持蓝牙功能
    if (adapter == null)
    {
      Toast.makeText(this, "该手机不支持蓝牙功能", Toast.LENGTH_LONG).show();
      return;
    }
    // 判断蓝牙功能是否打开
    if (!adapter.isEnabled())
    {
      // 强行打开
//      adapter.enable();
      Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivity(intent);
    }

    IntentFilter filter = new IntentFilter();
    // 注册扫描开始的广播
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    // 注册扫描结束的广播
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    // 注册扫描已经找到设备的广播
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    receiver = new MyReceiver();
    registerReceiver(receiver, filter);
    startBlueServer();
  }

  
  private void startBlueServer()
  {
    new Thread()
    {
      @Override
      public void run()
      {
 while (true)
 {
   try
   {
     BluetoothServerSocket serverSocket = adapter
  .listenUsingRfcommWithServiceRecord(
      "蓝牙服务",
      UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
     mSocket = serverSocket.accept();//阻塞
     BlueToothHandler.getInstance().setSocket(mSocket);
   }
   catch (IOException e)
   {
     e.printStackTrace();
   }
 }
      }
    }.start();
  }

  public void doClick(View view)
  {
    switch (view.getId())
    {
      case R.id.btn:
 mDevices.clear();
 mDeviceAdapter.notifyDataSetChanged();
 // 判断是否正在扫描,如果没在扫描那么就开始扫描
 if (!adapter.isDiscovering() && adapter.startDiscovery())
 {
   Toast.makeText(this, "开始扫描周围的蓝牙设备", Toast.LENGTH_LONG)
.show();
 }
 break;
      case R.id.send:
 if ("".equals(et.getText().toString()))
 {
   Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
   return;
 }
 if (BlueToothHandler.getInstance().getSocket() == null)
 {
   Toast.makeText(this, "还未连接设备", Toast.LENGTH_SHORT).show();
   return;
 }
 new Thread()
 {
   @Override
   public void run()
   {
     BlueToothHandler.getInstance().sendMessage(
  et.getText().toString());
   }
 }.start();
 break;
      case R.id.dis:
 BlueToothHandler.getInstance().closeSocket();
 break;
    }
  }

  @Override
  protected void onDestroy()
  {
    super.onDestroy();
    if (receiver != null)
    {
      // 注销广播
      unregisterReceiver(receiver);
    }
  }

  private void initListView()
  {
    mDevices = new ArrayList();
    mDeviceAdapter = new com.example.se7en.testbluetooth.DeviceAdapter(this, mDevices);
    mListView.setAdapter(mDeviceAdapter);
    mListView.setonItemClickListener(new AdapterView.onItemClickListener()
    {
      @Override
      public void onItemClick(AdapterView parent, View view,
   int position, long id)
      {
 try
 {
   mSocket = mDevices
.get(position)
.createRfcommSocketToServiceRecord(
    UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
   mSocket.connect();//阻塞
   Toast.makeText(MainActivity.this, "连接成功",
Toast.LENGTH_SHORT).show();
   BlueToothHandler.getInstance().setSocket(mSocket);
 }
 catch (IOException e)
 {
   e.printStackTrace();
 }

      }
    });
  }

  public class MyReceiver extends BroadcastReceiver
  {

    @Override
    public void onReceive(Context context, Intent intent)
    {
      String action = intent.getAction();
      if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
      {
 Log.i("info", "开始扫描");
      }
      else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
      {
 Log.i("info", "扫描结束");
      }
      else if (action.equals(BluetoothDevice.ACTION_FOUND))
      {
 BluetoothDevice device = intent
     .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 Log.i("info", device.getName() + ":" + device.getAddress());
 if (mDevices != null)
 {
   for (int i = 0; i < mDevices.size(); i++)
   {
     if (device.getAddress()
  .equals(mDevices.get(i).getAddress()))
     {
return;
     }
   }
   mDevices.add(device);
   mDeviceAdapter.notifyDataSetChanged();
 }
      }
    }

  }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/161277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号