[代码共享]软件《设备扩展扩音系统》代码(篇1)
Private Sub Form_Load()'窗体载入事件,form是窗体名称,private是私有过程,public是公共过程。
SndPlaySound
End Sub'当前过程结束,类似的还有end if,其中end if跟if…then语句相匹配,是if判断语句的结束标志,而end sub是整个过程的结束标志。
Private Sub Command1_Click()'按钮单击事件,command1是按钮对象名,click是对应对象的单击事件,类似的还有dbclick,按钮双击事件等。
End Sub
VB6.0
//Java-MainActivity
//Java-settings
//Java-audio_input
<!--XML-activity_main-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="end">
<Button<!--控件:按钮-->
android:id="@+id/启动服务"<!--设置按钮ID-->
android:onClick="onclick"<!--设置按钮的onclick方法-->
android:text="启动服务"<!--设置按钮文本-->
android:background="@android:color/transparent"<!--设置按钮背景颜色-->
android:layout_width="wrap_content"<!--设置按钮的宽度属性-->
android:drawableLeft="@drawable/开始服务"<!--设置按钮图标-->
/>
<Button
android:text="设置"
android:id="@+id/设置"
android:background="@android:color/transparent"
android:onClick="main"
android:layout_width="wrap_content"
android:layout_marginRight="0dp"
android:drawableLeft="@drawable/settings_activity" />
</LinearLayout>
<!--XML-settings-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/设置列表"
android:text="设置"
android:layout_marginLeft="150px"
android:textSize="100px"
android:background="@android:color/transparent"
/>
<Button
android:id="@+id/选择输入源"
android:text="输入设置"
android:onClick="onclick"
android:textAlignment="viewStart"
android:tooltipText="该功能用于设置音频信号输入源。"
android:background="@android:color/transparent"
/>
<Button
android:id="@+id/投屏设置"
android:text="投屏选项"
android:tooltipText="该功能用于设置启动时是否自动投屏以及设置投屏信号输入源。"
android:textAlignment="viewStart"
android:onClick="onclick"
android:background="@android:color/transparent"
/>
<Button
android:id="@+id/歌曲元数据设置"
android:text="歌曲元数据选项"
android:tooltipText="设置传输音频信号时是否还传输音频播放列表中的歌曲元数据信息。"
android:onClick="onclick"
android:textAlignment="viewStart"
android:background="@android:color/transparent"
/>
<Button
android:id="@+id/连接方式"
android:text="设置连接方式"
android:onClick="onclick"
android:tooltipText="设置音频或视频的输出方式。"
android:textAlignment="viewStart"
android:background="@android:color/transparent"
/>
</LinearLayout>
<!--XML-audio_imput-->
<!--XML-video_input-->
<!--XML-connect_-->
<!--XML-music_datalist-->
Android主程序
//Java
麦克风驱动
//Java-MainActivity
package com.controller.app;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;//导入蓝牙适配器API
import android.widget.Toast;//导入toast模块
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.UnsupportedEncodingException;
import android.bluetooth.BluetoothDevice;
import java.net.SocketException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import android.content.IntentFilter;
import android.app.Activity;
import android.util.Log;
import java.util.HashMap;
import java.io.InputStreamReader;
import android.content.Context;
import java.util.UUID;
import com.controller.app.R;
public class MainActivity extends AppCompatActivity {
private static final String MY_UUID1 = "00001101-0000-1000-8000-00805F9B34FB";
UUID MY_UUID = UUID.fromString(MY_UUID1);
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
public void sendBluetoothData(View view) {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show();
return;
}
BluetoothDevice device = getPairedDevice();
if (device == null) {
Toast.makeText(this, "No paired devices found", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
public void run() {
try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
String data = "Hello, Bluetooth!";
sendData(data);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Data sent successfully", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Error sending data", Toast.LENGTH_SHORT).show();
}
});
} finally {
closeResources();
}
}
}).start();
}
public void main(String[] args){
String data = "测试";
try{
sendData(data);
}catch (UnsupportedEncodingException e3){
System.err.println("无法发送"+e3.getMessage());
}
}
public void back(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void menu(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void OK(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void quit(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonTbuttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void allleft(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void bluetoothleft(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void usbleft(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void WiFileft(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void allright(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void allright(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void bluetoothright(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void USBright(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void WiFiright(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void mutebluetooth(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void mute(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void searchormuteWiFi(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,",当你不处在电视模式状态下时,该按钮功能将会自动静音全部的WiFi设备。",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedIntanceState) {
super.onCreate(savedIntanceState);
setContentView(R.layout.controller);
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(MainActivity.this, "对不起,您的设备不支持蓝牙,不能使用遥控器!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
finish();
return;
}else if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "请先开启蓝牙!本遥控器是基于蓝牙进行操作的!请在启动蓝牙后,再启动遥控器!\n遥控器自动重启还在调试阶段,给您带来不便,请谅解!\n遥控器发明人:江苏省海门中等专业学校 信息系 22CZ02 沈诗淳", Toast.LENGTH_LONG).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);finish();
return;
}else{
Toast.makeText(MainActivity.this, "遥控器发明人:江苏省海门中等专业学校 信息系 22CZ02 沈诗淳", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"请不要连续点击按键!否则可能会一段时间内不响应,反而导致长时间弹窗!",Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"当你不在电视模式下时,某些按键可能执行斜杠后面的功能。",Toast.LENGTH_SHORT).show();
}
Button button = findViewById(R.id.遥控器设置);//导入按钮控件,通过导入按钮控件变量加载按钮。
Button button1 = findViewById(R.id.下一个面板);
Button button2 =findViewById(R.id.信息);
Button button3 = findViewById(R.id.TVAV切换);
Button button4 = findViewById(R.id.上);
Button button5 = findViewById(R.id.下);
Button button6 =findViewById(R.id.左);
Button button7 = findViewById(R.id.右);
Button button8 = findViewById(R.id.立体声);
Button button9 = findViewById(R.id.单声道);
Button button10 =findViewById(R.id.静音);
Button button11 = findViewById(R.id.全部加);
Button button12 = findViewById(R.id.蓝牙加);
Button button13 = findViewById(R.id.USB加);
Button button14 =findViewById(R.id.WiFi加);
Button button15 = findViewById(R.id.全局减);
Button button16 = findViewById(R.id.蓝牙减);
Button button17 = findViewById(R.id.USB减);
Button button18 =findViewById(R.id.WiFi减);
Button button19 = findViewById(R.id.播放暂停切换);
Button button20 = findViewById(R.id.停止);
Button button21 = findViewById(R.id.上一曲);
Button button22 =findViewById(R.id.下一曲);
Button button23 = findViewById(R.id.数字键1);
Button button24 = findViewById(R.id.数字键2);
Button button25 = findViewById(R.id.数字键3);
Button button26 =findViewById(R.id.数字键4);
Button button27 = findViewById(R.id.数字键5);
Button button28 = findViewById(R.id.数字键6);
Button button29 = findViewById(R.id.数字键7);
Button button30 =findViewById(R.id.数字键8);
Button button31 = findViewById(R.id.数字键9);
Button button32 = findViewById(R.id.数字键0);
Button button33 = findViewById(R.id.设备加);
Button button34 = findViewById(R.id.设备减);
Button button35 = findViewById(R.id.话筒加);
Button button36 = findViewById(R.id.话筒减);
Button button37 = findViewById(R.id.快进);
Button button38 = findViewById(R.id.快退);
Button button39 =findViewById(R.id.图像设置);
Button button40 = findViewById(R.id.仪表板切换);
Button button41 = findViewById(R.id.打开歌词窗口);
Button button42 = findViewById(R.id.自动搜索);
Button button43 =findViewById(R.id.静音蓝牙设备);
Button button44 = findViewById(R.id.静音);
Button button45 = findViewById(R.id.菜单);
button1.setOnClickListener(new View.OnClickListener() {//设置单击监听器以设置单击事件
@Override//标识符
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this,next.class);//使用intent标签指定并跳转界面。
startActivity(intent);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
Toast.makeText(MainActivity.this,"Testing tools,please wait for the later version.",Toast.LENGTH_SHORT).show();
//Intent intent2 =new Intent(MainActivity.this,settings.class);
//startActivity(intent2);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button2.getId());//将按钮控件的ID导入到字符串变量buttontext中
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();//使用toast+转义字符+变量+字符串进行组合显示
}
}
});
button3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button3.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button4.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button5.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button6.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button7.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button7.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button8.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button8.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button9.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button10.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button11.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button12.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button13.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button14.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button15.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button16.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button17.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button18.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button19.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button20.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button21.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button22.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button23.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button24.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button25.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button26.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button27.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button28.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button29.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button30.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button31.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button12.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button33.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button34.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button35.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button36.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button37.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button38.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button39.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button40.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button41.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
button42.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(button10.getId());
try {
sendData(buttonText);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// 蓝牙已开启
// 重新启动应用
Intent intent = getPackageManager().getLaunchIntentForPackage("com.controller.app");
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
} else {
// 用户拒绝了开启蓝牙请求
// 退出应用
System.exit(0);
}
}
}
public final BroadcastReceiver bluetoothhkskdoke = new BroadcastReceiver(){
private BluetoothDevice pairingDevice;
@Override
public void onReceive(Context context,Intent intent){
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int bootstate = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
int previewbootstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,BluetoothDevice.ERROR);
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(bluetoothhkskdoke,intentFilter);
if (bootstate==BluetoothDevice.BOND_BONDED){
pairingDevice = device;
try {
String uuidString = "00001101-0000-1000-8000-00805F9B34FB";
UUID MY_UUID = UUID.fromString(uuidString);
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
}catch (IOException e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Connecting failed!",Toast.LENGTH_SHORT).show();
}
}else if (bootstate==BluetoothDevice.BOND_NONE && previewbootstate == BluetoothDevice.BOND_BONDED){
pairingDevice = null;
Toast.makeText(MainActivity.this,"已取消连接到接收端设备。",Toast.LENGTH_SHORT).show();
}
}
}
};
public void handleButtonClick(String command){
if (bluetoothSocket == null || !bluetoothSocket.isConnected()){
Toast.makeText(MainActivity.this,"设备未连接。",Toast.LENGTH_SHORT).show();
return;
}else if(outputStream ==null){
Toast.makeText(MainActivity.this,"输出流未初始化。",Toast.LENGTH_SHORT).show();
return;
}
}
private static final Logger catcher = Logger.getLogger(MainActivity.class.getName());
public void sendData(String Command) throws UnsupportedEncodingException{
if (bluetoothAdapter==null || !bluetoothAdapter.isEnabled()){
Toast.makeText(MainActivity.this, "提示:蓝牙不可用,请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}else if (bluetoothAdapter.isEnabled()){
BluetoothDevice device = getPairedDevice();
if (device==null){
Toast.makeText(MainActivity.this,"设备未配对。",Toast.LENGTH_SHORT).show();
return;
}
}
byte[] commandbytes = Command.getBytes("UTF-8");
byte[] packet = new byte[1 + 1 + commandbytes.length];
packet[0] = (byte) 0xAA;//包头。
packet[1] = (byte) commandbytes.length;//设置命令长度。
System.arraycopy(commandbytes, 0, packet, 2, commandbytes.length);
try {
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
try {
outputStream.write(packet);
}catch (IOException e){
Toast.makeText(MainActivity.this,"Error!Sending controlling data failed!",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(bluetoothhkskdoke);
if (bluetoothSocket !=null && bluetoothSocket.isConnected()){
try {
bluetoothSocket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
private BluetoothDevice getPairedDevice(){
if (bluetoothAdapter != null){
return bluetoothAdapter.getBondedDevices().iterator().next();
}return null;
}
private void closeResources(){
try {
if (outputStream != null){
outputStream.close();
}else if (bluetoothSocket != null){
bluetoothSocket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
private final BroadcastReceiver bluetoothkskdoke = new BroadcastReceiver(){//定义一个服务端。
private BluetoothDevice pairingDevice;
@Override
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int bootstate = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
int previewbootstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,BluetoothDevice.ERROR);
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(bluetoothkskdoke,intentFilter);
if (bootstate==BluetoothDevice.BOND_BONDED){
/*连接成功*/
pairingDevice = device;
try {
String uuidString = "00001101-0000-1000-8000-00805F9B34FB";
UUID MY_UUID = UUID.fromString(uuidString);
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
}catch (IOException e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Connecting failed!",Toast.LENGTH_SHORT).show();
}
}else if (bootstate==BluetoothDevice.BOND_NONE && previewbootstate == BluetoothDevice.BOND_BONDED){
/*取消连接*/
pairingDevice = null;
Toast.makeText(MainActivity.this,"已取消连接到接收端设备。",Toast.LENGTH_SHORT).show();
}
}
}
};
}
//Java-next
package com.controller.app;
import java.nio.charset.StandardCharsets;
import android.app.Activity;
import android.util.Log;
import java.util.logging.Level;
import java.util.logging.Logger;
import android.app.Activity;
import java.util.HashMap;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.widget.Toast;
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import android.content.Context;
import java.nio.charset.StandardCharsets;
import java.io.Reader;
import java.util.UUID;
import java.util.Set;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.Context;
import java.io.UnsupportedEncodingException;
public class next extends Activity {
private static final String MY_UUID1 = "00001101-0000-1000-8000-00805F9B34FB";
private BluetoothAdapter bluetoothAdapter;
UUID MY_UUID = UUID.fromString(MY_UUID1);
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
private int shiftmode = 0;
private int Ctrlmode = 0;
private Context context;
public void sendBluetoothData(View view) {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show();
return;
}
BluetoothDevice device = getPairedDevice();
if (device == null) {
Toast.makeText(this, "No paired devices found", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
public void run() {
try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
String data = "Hello, Bluetooth!";
sendData(data);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(next.this, "Data sent successfully", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(next.this, "Error sending data", Toast.LENGTH_SHORT).show();
}
});
} finally {
closeResources();
}
}
}).start();
}
public void main(String[] args){
String data = "测试";
try{
sendData(data);
}catch (UnsupportedEncodingException e3){
System.err.println("无法发送"+e3.getMessage());
}
}
public void Permutation(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void Combination(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
if (shiftmode==1){
if (Ctrlmode==1){
String buttonText = "storage";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}else if (Ctrlmode==0){
String buttonText = "Rcall";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}else if (shiftmode==0){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
}
public void A(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText;
if (shiftmode==0){
buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}else if (shiftmode==1){
buttonText = "X";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
}
public void B(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText;
if (shiftmode==0){
buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}else if (shiftmode==1){
buttonText = "Y";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
}
public void C(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText;
if (shiftmode==0){
buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}else if (shiftmode==1){
buttonText = "Z";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
}
public void D(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void E(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText;
if (shiftmode==0){
buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}else if (shiftmode==1){
buttonText = "MDF";
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
}
public void F(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void π(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void delete(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void changecomputingmode(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void setpassword(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(next.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
sendData(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(next.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
//Java-third
package com.controller.app;
import android.app.Activity;
import android.content.IntentFilter;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.widget.Toast;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothDevice;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.UnsupportedOperationException;
import java.net.SocketException;
import java.io.OutputStream;
import java.io.Reader;
import java.util.Set;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import java.util.UUID;
import com.controller.app.R;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;
import java.io.UnsupportedEncodingException;
public class third extends Activity {
private static final String MY_UUID1 = "00001101-0000-1000-8000-00805F9B34FB";
UUID MY_UUID = UUID.fromString(MY_UUID1);
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
private int shiftmode = 0;
private Context context;
public void sendBluetoothData(View view) {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show();
return;
}
BluetoothDevice device = getPairedDevice();
if (device == null) {
Toast.makeText(this, "No paired devices found", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
public void run() {
try {
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
String data = "Hello, Bluetooth!";
senddata(data);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(third.this, "Data sent successfully", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(third.this, "Error sending data", Toast.LENGTH_SHORT).show();
}
});
} finally {
closeResources();
}
}
}).start();
}
public void main(String[] args){
String data = "测试";
try {
senddata(data);
}catch (UnsupportedEncodingException e){
System.err.println("UnsupportedEncodingException occurred in :"+e.getMessage());
}
}
public void TV_volume_down(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void TV_volume_up(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void audio_channel_switching(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void mute(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void channel_replay(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void FMplay_up(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void FMplay_down(View view){
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(third.this, "提示:请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
return;
}else if (bluetoothAdapter.isEnabled()){
String buttonText = getResources().getResourceEntryName(view.getId());
try {
senddata(buttonText);
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
}
Toast.makeText(third.this,"已发送指令\""+buttonText+"\"", Toast.LENGTH_SHORT).show();
}
}
public void last(View view){
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
bluetoothAdapter = bluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "对不起,您的设备不支持蓝牙,不能使用遥控器!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
finish();
return;
}else if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
finish();
return;
}
}
private final BroadcastReceiver bluetoothkskdoke = new BroadcastReceiver(){//定义一个服务端。
private BluetoothDevice pairingDevice;
@Override
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int bootstate = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
int previewbootstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,BluetoothDevice.ERROR);
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(bluetoothkskdoke,intentFilter);
if (bootstate==BluetoothDevice.BOND_BONDED){
/*连接成功*/
pairingDevice = device;
try {
String uuidString = "00001101-0000-1000-8000-00805F9B34FB";
UUID MY_UUID = UUID.fromString(uuidString);
bluetoothSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
bluetoothSocket.connect();
outputStream = bluetoothSocket.getOutputStream();
}catch (IOException e){
e.printStackTrace();
Toast.makeText(third.this,"Connecting failed!",Toast.LENGTH_SHORT).show();
}
}else if (bootstate==BluetoothDevice.BOND_NONE && previewbootstate == BluetoothDevice.BOND_BONDED){
/*取消连接*/
pairingDevice = null;
Toast.makeText(third.this,"已取消连接到接收端设备。",Toast.LENGTH_SHORT).show();
}
}
}
};
private void handleButtonClick(String command){
if (bluetoothSocket == null || !bluetoothSocket.isConnected()){
Toast.makeText(third.this,"设备未连接。",Toast.LENGTH_SHORT).show();
return;
}else if(outputStream ==null){
Toast.makeText(third.this,"输出流未初始化。",Toast.LENGTH_SHORT).show();
return;
}
}
private static final Logger catcher = Logger.getLogger(third.class.getName());
private void senddata(String Command) throws UnsupportedEncodingException{
if (bluetoothAdapter==null || !bluetoothAdapter.isEnabled()){
Toast.makeText(third.this, "提示:蓝牙不可用,请先开启蓝牙!本遥控器是基于蓝牙进行操作的!", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}else if (bluetoothAdapter.isEnabled()){
BluetoothDevice device = getPairedDevice();
if (device==null){
Toast.makeText(third.this,"设备未配对。",Toast.LENGTH_SHORT).show();
return;
}
}
byte[] commandbytes = Command.getBytes("UTF-8");
byte[] packet = new byte[1 + 1 + commandbytes.length];
packet[0] = (byte) 0xAA;//包头。
packet[1] = (byte) commandbytes.length;//设置命令长度。
System.arraycopy(commandbytes, 0, packet, 2, commandbytes.length);
try {
}catch(Exception e2){
catcher.log(Level.SEVERE,"通信错误。",e2);
e2.printStackTrace();
try {
outputStream.write(packet);
}catch (IOException e){
Toast.makeText(third.this,"Error!Sending controlling data failed!",Toast.LENGTH_SHORT).show();
}
}
}
private BluetoothDevice getPairedDevice(){
if (bluetoothAdapter != null){
return bluetoothAdapter.getBondedDevices().iterator().next();
}return null;
}
private void closeResources(){
try {
if (outputStream != null){
outputStream.close();
}else if (bluetoothSocket != null){
bluetoothSocket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(bluetoothkskdoke);
if (bluetoothSocket !=null && bluetoothSocket.isConnected()){
try {
bluetoothSocket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
功放遥控器
下一篇篇2 点击查看>>