android蓝牙通信发送字符串,Android示例蓝牙代码可通过蓝牙发送简单的字符串

我编写了以下代码,即使是初学者也可以理解。只需复制代码并阅读注释即可。请注意,要发送的消息被声明为全局变量,您可以在发送消息之前对其进行更改。常规更改可以在Handler函数中完成。

multiplayerConnect.java

import android.annotation.SuppressLint;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothServerSocket;

import android.bluetooth.BluetoothSocket;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.support.annotation.Nullable;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Set;

import java.util.UUID;

public class multiplayerConnect extends AppCompatActivity {

public static final int REQUEST_ENABLE_BT=1;

ListView lv_paired_devices;

Set set_pairedDevices;

ArrayAdapter adapter_paired_devices;

BluetoothAdapter bluetoothAdapter;

public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

public static final int MESSAGE_READ=0;

public static final int MESSAGE_WRITE=1;

public static final int CONNECTING=2;

public static final int CONNECTED=3;

public static final int NO_SOCKET_FOUND=4;

String bluetooth_message="00";

@SuppressLint("HandlerLeak")

Handler mHandler=new Handler()

{

@Override

public void handleMessage(Message msg_type) {

super.handleMessage(msg_type);

switch (msg_type.what){

case MESSAGE_READ:

byte[] readbuf=(byte[])msg_type.obj;

String string_recieved=new String(readbuf);

//do some task based on recieved string

break;

case MESSAGE_WRITE:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值