Day6 蓝牙开启、搜索、关闭、连接

本文介绍了如何在主界面上操作蓝牙,包括开启、搜索、关闭及连接设备,并通过服务端和客户端线程实现消息发送的功能。
摘要由CSDN通过智能技术生成

主界面

package com.example.mp;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
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.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mp.thread.ClientThread;
import com.example.mp.thread.ServiceThread;

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

public class MainActivity extends AppCompatActivity {
   
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothManager bluetoothManager;
    private Button mainButtonOpen;
    private Button mainButtonClose;
    private Button mainButtonSearch;
    private Button mainButtonShow;
    private ListView mainListLeft;
    private ListView mainListRight;
    private ArrayAdapter aaa;
    private ArrayAdapter bbb;
    private List<String> name=new ArrayList<>();
    private List<String> devices=new ArrayList<String>();
    private List<BluetoothDevice> left_devices=new ArrayList<BluetoothDevice>();
    private List<BluetoothDevice> device=new ArrayList<BluetoothDevice>();
    private BaseReceiver br;

    @SuppressLint("HandlerLeak")
    private Handler han=new Handler(){
   
        @Override
        public void handleMessage(Message msg) {
   
            super.handleMessage(msg);
            if( msg.what==102){
   
                Toast.makeText(MainActivity.this, ""+msg.obj, Toast.LENGTH_SHORT).show();
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值