• 大小: 10KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: Java
  • 标签:

资源简介

最简单、功能最全的蓝牙类,只需要继承蓝牙activity实现接口即可。让蓝牙开发变得简单高效。

资源截图

代码片段和文件信息

package com.mypeam.cjwddz.mpeam;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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 java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * Created by cjwddz on 2016/8/21.
 */
public abstract class BleActivity extends Activity{

    public static BluetoothAdapter bleadt =BluetoothAdapter.getDefaultAdapter();
    private static final UUID PRIVATE_UUID = UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB“);
    private static BluetoothSocket socket=null;
    private static InputStream in=null;
    private static OutputStream out=null;

    BroadcastReceiver blebroadcast=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context Intent intent) {
            switch (intent.getAction()){
                case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                    finishSearch();
                    break;
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                   connectedSuccess();
                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    lostconnect();
                    break;
                case BluetoothDevice.ACTION_FOUND:
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    deviceFound(device);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bleadt =BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter=new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(blebroadcastfilter);

        if(islinking())
            reciveThread();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(blebroadcast);
        close();
        super.onDestroy();
    }

    public abstract void finishSearch();
    public abstract void connectedSuccess();
    public abstract void lostconnect();
    public abstract void deviceFound(BluetoothDevice device);
    public abstract void connectFailed();
    public abstract void bleMsg(byte[] msg);


    //打开蓝牙
    public void enableBle(boolean force)
    {
        if(!bleadt.isEnabled())
        {
            if(force)
                bleadt.enable();
            else
            {
                Intent enabler = new I

评论

共有 条评论