Android中的蓝牙打印机连接失败 [英] Bluetooth Printer Connection Failed in Android

查看:173
本文介绍了Android中的蓝牙打印机连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个POS应用程序.在此应用中,我需要打印收据,但是蓝牙连接有问题.

I want to make a POS app. In this app, I need to print a receipt, but I have a problem with the bluetooth connection.

在此代码中,我想设置在片段中使用的打印机设备.我希望即使我移到另一个片段,蓝牙也能保持连接状态.我将代码放在MainActivity中,但是问题是每次我移至另一个片段时,mService始终为null.所以我无法连接到设备.

In this code, I want to set the printer device that I use in my fragment. I want the bluetooth to stay connected even though I move to another fragment. I put the code in my MainActivity, but the problem is every time I move to another fragment, mService is always null. So I couldn't connect to the device.

请帮助我.预先感谢.

MainActivity.Java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{
    private static final String TAG = "MainActivity";
    Fragment fragment = null;
    Class fragmentClass = null;

    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;
    public static final int MESSAGE_CONNECTION_LOST = 6;
    public static final int MESSAGE_UNABLE_CONNECT = 7;
    private String mConnectedDeviceName = null;
    // Key names received from the BluetoothService Handler
    public static final String DEVICE_NAME = "device_name";
    public static final String TOAST = "toast";

    public static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;

    private BluetoothAdapter mBluetoothAdapter = null;
    public BluetoothService mService = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        fragmentClass = RegisterFragment.class;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_main2, fragment).commit();

        Log.v(TAG, "Starting DoDaily service...");
        startService(new Intent(this, DoDaily.class));
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        FragmentManager fm = getSupportFragmentManager();

        //if you added fragment via layout xml



        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            // Otherwise, setup the session
        } else {
            if (mService == null) {
                setMService();
            }
        }

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main2, menu);
        return true;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        String TAG_FRAGMENT="";

        if (id == R.id.nav_regist) {
            fragmentClass = RegisterFragment.class;
            TAG_FRAGMENT ="register";
            // Handle the camera action
        } else if (id == R.id.nav_activity) {
            fragmentClass = ActivityFragment.class;
            TAG_FRAGMENT ="activity";
        }
        else if(id == R.id.nav_inventory)
        {
            fragmentClass = InventoryFragment.class;
            TAG_FRAGMENT ="inventory";
        }
        else if (id == R.id.nav_manage) {
            fragmentClass = SettingFragment.class;
            TAG_FRAGMENT ="setting";
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_main2, fragment,TAG_FRAGMENT).commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_STATE_CHANGE:
                    if (DEBUG)
                        Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:
                            //btnScan.setText(getText(R.string.Connecting));
                            fragment1.btnScanEnable(false);
                            break;
                        case BluetoothService.STATE_CONNECTING:
                            Toast.makeText(MainActivity.this,R.string.title_connecting,Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothService.STATE_LISTEN:
                        case BluetoothService.STATE_NONE:
                            Toast.makeText(MainActivity.this,R.string.title_not_connected,Toast.LENGTH_SHORT).show();
                            break;
                    }
                    break;
                case MESSAGE_WRITE:

                    break;
                case MESSAGE_READ:

                    break;
                case MESSAGE_DEVICE_NAME:
                    // save the connected device's name

                    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                    Toast.makeText(MainActivity.this,"Connected to " + mConnectedDeviceName,
                            Toast.LENGTH_SHORT).show();
                   String text ="Connected to "+mConnectedDeviceName;
                    fragment1.setTvText(text);
                    break;
                case MESSAGE_TOAST:
                    Toast.makeText(MainActivity.this, msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();
                    break;
                case MESSAGE_CONNECTION_LOST:
                    Toast.makeText(MainActivity.this, "Device connection was lost",Toast.LENGTH_SHORT).show();
                    String text1 ="Not Connect to Any Device";
                    fragment1.setTvText(text1);
//                    editText.setEnabled(false);
//                    sendButton.setEnabled(false);
                    break;
                case MESSAGE_UNABLE_CONNECT:
                    Toast.makeText(MainActivity.this, "Unable to connect device",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public void onStart() {
        super.onStart();

        // If Bluetooth is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            // Otherwise, setup the session
        } else {
            if (mService == null){
                setMService();
            }
        }
    }

    @Override
    public synchronized void onResume() {
        super.onResume();

        if (mService != null) {

            if (mService.getState() == BluetoothService.STATE_NONE) {
                // Start the Bluetooth services
                mService.start();
            }
        }
    }

    @Override
    public synchronized void onPause() {
        super.onPause();
        if (DEBUG)
            Log.e(TAG, "- ON PAUSE -");
    }

    @Override
    public void onStop() {
        super.onStop();
        if (DEBUG)
            Log.e(TAG, "-- ON STOP --");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth services
        if (mService != null)
            mService.stop();
        if (DEBUG)
            Log.e(TAG, "--- ON DESTROY ---");
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (DEBUG)
            Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
            case REQUEST_CONNECT_DEVICE:{
                // When DeviceListActivity returns with a device to connect
                if (resultCode == Activity.RESULT_OK) {
                    // Get the device MAC address
                    String address = data.getExtras().getString(
                            DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                    // Get the BLuetoothDevice object
                    if (BluetoothAdapter.checkBluetoothAddress(address)) {
                        BluetoothDevice device = mBluetoothAdapter
                                .getRemoteDevice(address);
                        // Attempt to connect to the device
                        mService.connect(device);
                    }
                }
                break;
            }
            case REQUEST_ENABLE_BT:{
                // When the request to enable Bluetooth returns
                if (resultCode == Activity.RESULT_OK) {
                    // Bluetooth is now enabled, so set up a session
//                    FragmentManager fm1 = getSupportFragmentManager();
//                    SettingFragment fragment1 = (SettingFragment) fm1.findFragmentByTag("setting");
//                    fragment1.KeyListenerInit();
                    setMService();
                } else {
                    // User did not enable Bluetooth or an error occured
                    Log.d(TAG, "BT not enabled");
                    Toast.makeText(MainActivity.this, R.string.bt_not_enabled_leaving,Toast.LENGTH_SHORT).show();
                    onBackPressed();
                }
                break;
            }
        }
    }

    public void setMService()
    {
        mService = new BluetoothService(MainActivity.this, mHandler);
    }
}

SettingFragment.Java

public class SettingFragment extends Fragment {

public static final int REQUEST_CONNECT_DEVICE = 1;
private static final String CHINESE = "GBK";

SessionManagement sessionManagement;
DatabaseHandler db;

TextView tvConnected;
Button btnScan;
Button btnTest;

public SettingFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_setting, container, false);
    getActivity().setTitle("Setting");


    btnScan = (Button)rootview.findViewById(R.id.btnScan);
    btnTest = (Button) rootview.findViewById(R.id.btnTest);
    tvConnected = (TextView) rootview.findViewById(R.id.tvPrinterConnect);

    KeyListenerInit();


    return rootview;
}

public void setTvText(String text)
{
    tvConnected = (TextView) getActivity().findViewById(R.id.tvPrinterConnect);
    tvConnected.setText(text);
}

public void btnScanEnable(Boolean set)
{
    btnScan = (Button)getActivity().findViewById(R.id.btnScan);
    btnScan.setEnabled(set);
}

public void KeyListenerInit() {
    btnScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent serverIntent = new Intent(getActivity(), DeviceListActivity.class);
            startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
        }
    });
    ((MainActivity)getActivity()).setMService();
}
}

推荐答案

从片段KeyListenerInit方法中删除此行,因为您已经在Activity中初始化了对象,然后又为什么要初始化它.

remove this line from fragment KeyListenerInit method because you already initialize object in Activity then why you again intialize this.

((MainActivity)getActivity()).setMService();

并从onCreate方法中删除此代码,因为您已经在onStart方法中添加了此代码,因此无需在onCreate方法中:

and remove this code from onCreate method because you already add this code in onStart method so no need in onCreate method:

 if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the session
    } else {
        if (mService == null) {
            setMService();
        }
    }

这篇关于Android中的蓝牙打印机连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆