我正在尝试制作一个应用程序,用于创建包含用户输入文本的文件。应该使用蓝牙将相同的文件(使用相同的按钮)发送到笔记本电脑。 我已经成功创建了文件但是当启用蓝牙选择接收器时,应用程序自行关闭而没有任何错误或消息
package com.example.anukool.blutt;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int DISCOVER_DURATION = 500;
private static final int REQUEST_BLU =1;
EditText textmsg;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg=(EditText)findViewById(R.id.write);
}
public void sendViaBluetooth(View v){
try {
FileOutputStream fileout=openFileOutput("data.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
代码完全正常,直到此处,但此后,不规则性开始
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter == null){
Toast.makeText(this,"Bluetooth not supported on the device",Toast.LENGTH_LONG).show();
}
else
{
enableBluetooth();
}
} catch (Exception ex) {
Toast.makeText(this, (CharSequence) ex,Toast.LENGTH_LONG).show();
}
}
public void enableBluetooth() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,DISCOVER_DURATION);
startActivityForResult(intent,REQUEST_BLU);
}
代码甚至可以工作到上面,应用程序要求启用蓝牙,允许应用程序启用蓝牙后,应用程序关闭没有错误但蓝牙已启用
从我希望尝试打开手机的蓝牙设备列表的代码,并在点击我需要的那个之后,将文件“data.txt”发送到该蓝牙设备 然而,这种情况并没有发生......
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File f = new File(context.getFilesDir(),"data.txt");
if(f.exists())
{
Toast.makeText(context, "command file created successfully", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, "command file not created.", Toast.LENGTH_LONG).show();
}
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
PackageManager pm = getPackageManager();
List<ResolveInfo> appsList = pm.queryIntentActivities(intent,0);
if(appsList.size() >0)
{
String packageName = null;
String className = null;
boolean found = false;
for(ResolveInfo info : appsList)
{
packageName = info.activityInfo.packageName;
if(packageName.equals("com.android.bluetooth"))
{
className = info.activityInfo.name;
found = true;
break;
}
}
if(!found)
{
Toast.makeText(this,"Bluetooth not found",Toast.LENGTH_LONG).show();
}
else
{
intent.setClassName(packageName,className);
startActivity(intent);
}
}
else
{
Toast.makeText(this,"Bluetooth cancelled",Toast.LENGTH_LONG).show();
}
}
}
}
我是一个完整的新手,并没有在android studio中获得蓝牙工具的经验:p